Hi all,
I use a Service to make the login system I use an object to keep username, token, and the password. I save the values on LaginPage in the object from the service.
AuthService
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { map } from 'rxjs/operators';
@Injectable({
providedIn: 'root'
})
export class AuthService {
public credentials = {
username:'',
password:'',
token:''
}
constructor(public http:HttpClient) { }
LoginPage.html
<ion-content padding>
<ion-item>
<ion-label>
<ion-icon name="mail"></ion-icon>
</ion-label>
<ion-input clearInput placeholder="Username" type="text" [(ngModel)]="this.authService.credentials.username"></ion-input>
</ion-item>
<ion-item>
<ion-label>
<ion-icon name="lock"></ion-icon>
</ion-label>
<ion-input clearInput placeholder="Password" type="password" [(ngModel)]="this.authService.credentials.password"></ion-input>
</ion-item>
<ion-button padding expand="full" color="nicedarkgreen" (click)="Login()">Login</ion-button>
</ion-content>
LoginPage.ts
import { Component, OnInit } from '@angular/core';
import { NavController, AlertController } from '@ionic/angular';
import { Router } from '@angular/router';
import { Storage } from '@ionic/storage';
import { AuthService } from '../../services/auth.service';
@Component({
selector: 'app-login',
templateUrl: './login.page.html',
styleUrls: ['./login.page.scss'],
})
export class LoginPage implements OnInit {
constructor(public navCtrl: NavController, public routsCtrl:Router, public authService:AuthService,public storage: Storage,
private alertCtrl: AlertController,private ordersService:OrdersService) { }
ngOnInit(){
if(!this.authService.credentials.username)
{
this.storage.get('username').then((val) => {
if(val)
{
this.authService.credentials.username = val;
this.storage.get('password').then((val) => {
if(val)
{
this.authService.credentials.password = val;
}
});
}
});
}
console.log(this.authService.credentials);
}
Login()
{
let newCredentials;
if(!this.authService.credentials.username || !this.authService.credentials.password)
{
this.PresentAlert('Missing username or password','Enter the username and the password.');
}else{
newCredentials = {"username":this.authService.credentials.username,"password":this.authService.credentials.password
};
this.storage.set('username',this.authService.credentials.username);
this.storage.set('password',this.authService.credentials.password);
}
this.authService.Login(newCredentials).then(()=>{
if(this.authService.canActivate())
{
this.storage.set('token', this.authService.credentials.token);
console.log(this.authService.credentials);
this.ordersService.PresentLoadingDefault();
this.ordersService.GetOrders().then(()=>
{
this.ordersService.GetArchive().then(()=>{
this.routsCtrl.navigateByUrl('/tabs/dashboard/(home:home)');
this.ordersService.loading.dismiss();
});
});
}else
{
this.routsCtrl.navigate(['login'])
}
},(err) =>{
});
}
On LoginPage I can view the correct values for “credentials” but when I access the values on a different page it returns the default value which is a blank string.