Hi guys,
I have a slight problem with login, although the login script is working and communicating with Laravel Passport I can see the Bearer auth token etc, but the script doesn’t then redirect to the dashboard even though I have specified this.
I guess this is something really simple I’ve missed or mis-understood, sorry Ionic newbie here…
So I have
if (isAuthenticated) {
this.presentToast("Success! You are now logged in");
this.navCtrl.navigateForward('/dashboard'); // My understanding if authenticated then redirect to dashboard page???
}
Here’s the full code
import { Component, OnInit } from '@angular/core';
import { NavController } from '@ionic/angular';
import { AuthProvider } from '../../../../providers/auth/auth';
import { Toast } from '@ionic-native/toast/ngx';
@Component({
selector: 'app-login',
templateUrl: './login.page.html',
styleUrls: ['./login.page.scss'],
})
export class LoginPage implements OnInit {
loading: any;
formLogin: any = {
email: '',
password: '',
};
constructor(
public authService: AuthProvider,
private navCtrl: NavController,
public toast: Toast,
) { }
ngOnInit() {
}
async checkAuthenticated() {
try {
let isAuthenticated = await this.authService.checkIsAuthenticated();
if (isAuthenticated) {
this.presentToast("Success! You are now logged in");
this.navCtrl.navigateForward('/dashboard');
}
} catch (err) {
this.presentToast('Error on verify authentication info');
}
}
login(data: any) {
this.authService.login(data)
.then((response: any) => {
this.presentToast("Checking Credentials");
this.authService.storeCredentials(response);
setTimeout(() => {
this.checkAuthenticated()
}, 750);
})
.catch(err => {
this.presentToast('Error');
if (err.status == 400) {
this.presentToast("Please check you have entered your email and password!");
} else if (err.status == 401) {
this.presentToast("401 Error");
} else {
console.log(err.error.message);
this.presentToast(err.error);
}
})
}
presentToast(msg: string) {
this.toast.show(msg, '5000', 'top').subscribe(
toast => {
// console.log(toast);
}
);
}
}