Hi guys,
This is what I have:
I have a very simple app with 3 pages:
- LoginPage
- HomePage
- HomeDetailPage ( I call it from HomePage with navController.push(HomeDetailPage) )
And this is what I want:
I want that when the user do login, the app should redirect He to the HomePage.
If the user is in the HomePage or in HomeDetailPage and launch logout function, the app should redirect he to the LoginPage and if He’ll make login again, The app will go in the HomePage.
But this is my trouble:
If I call logout function from the HomePage all is well.
But If I call the logout function from the HomeDetailPage, the App come back to LoginPage correctly but if i try to call login again, I recive an error:
ERROR TypeError: Cannot read property 'push' of null
at Tab.NavControllerBase._queueTrns (vendor.js:51240)
at Tab.NavControllerBase.push (vendor.js:51128)
at SafeSubscriber._next (main.js:259)
at SafeSubscriber.__tryOrUnsub (vendor.js:22842)
at SafeSubscriber.next (vendor.js:22789)
at Subscriber._next (vendor.js:22729)
at Subscriber.next (vendor.js:22693)
at MapSubscriber._next (vendor.js:124303)
at MapSubscriber.Subscriber.next (vendor.js:22693)
at SwitchMapSubscriber.notifyNext (vendor.js:129085)
app.component.ts
I’ve used AngularFireAuth for make login and logoud and check the authentication
import { Component, ViewChild } from '@angular/core';
import { Platform, NavController } from 'ionic-angular';
import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';
import { TabsPage } from '../pages/tabs/tabs';
import { LoginPage } from '../pages/login/login';
import { AngularFireAuth } from 'angularfire2/auth';
@Component({
templateUrl: 'app.html'
})
export class MyApp {
rootPage: any = LoginPage;
@ViewChild('rootNav') nav: NavController;
constructor(platform: Platform,
statusBar: StatusBar,
splashScreen: SplashScreen,
private afAuth: AngularFireAuth) {
platform.ready().then(() => {
//I've tried to move here the block of code in the ngOnInit but the behavieour is the same
statusBar.styleDefault();
splashScreen.hide();
});
}
ngOnInit() {
this.afAuth.auth.onAuthStateChanged(user => {
if (user) {
this.nav.setRoot(HomePage); //I've tried also with this.rootPage = HomePage and the behavieur is the same
} else {
this.nav.setRoot(LoginPage); //I've tried also with this.rootPage = LoginPage and the behavieur is the same
}
});
}
}
The Login function in login.ts page is:
login(email: string, password: string) {
this.afAuth
.auth
.signInWithEmailAndPassword(email, password)
.then(value => { })
.catch(err => {
console.log('Something went wrong:', err.message);
});
}
And the logout function iinside the HomePage.ts and DetailHomePage is:
logout() {
if (this.navCtrl.last().index > 0) {
this.navCtrl.remove(this.navCtrl.last().index)
.then( () => {
this.afAuth.auth.signOut();
},
error => {console.error(error); }
);
}
else {
this.authService.logout();
}
I’ve tried with a simple logout version like this below also, but the error that I have had is the same.
logout() {
this.afAuth.auth.signOut();
}
Thank you