jeffminsungkim / Ashy

0 stars 0 forks source link

Tabs persisting after logging out of app #6

Closed jeffminsungkim closed 6 years ago

jeffminsungkim commented 6 years ago

Bottom tabs persist after signing out of app. The following code below solves a fundamental tab persistency issue. However, it looks like a wrong approach.

import { App } from 'ionic-angular';
constructor( public app: App ) { }

async logout() {
    const user: any = await this.authService.signOut();
    this.app.getRootNav().setRoot('LoginPage'); // Better way to fix this line?
    this.toastService.show(`Signed out as ${user.email}`);
  }
jeffminsungkim commented 6 years ago

According to the following post I figured it out a better way to handle both tabs and user sign out.

app.component.ts

afAuth.auth.onAuthStateChanged(user => {
        console.log("App User", user);
        if (user) {
          this.rootPage = 'HomePage'; // Tabs reside here
          toastService.show(`Signed in as ${user.email}`);
        } else {
          this.rootPage = 'LoginPage';
        }
      });

onAuthStateChanged() this method will monitor authentication state which takes in a callback. And this callback fires off every single time the authentication state changes. So whether a user logs in or a user logs out that will trigger user => {} this callback function.

setting.ts

async logout() {
    const user: any = await this.authService.signOut();
    this.toastService.show(`Signed out as ${user.email}`);
  }

Therefore, I don’t need to use this.app.getRootNav().setRoot('LoginPage'); anymore. Also, I don’t need to consider the bottom tabs. Firebase will take care of it everything for me.