after I successfully log into my app using Firebase I want to store a bunch of information (like, user email, user uid, user name...) and use it throughout my app. The best way I found for this is to use Ionic Storage.

Now, in the first login works fine, but If I log out and log in with another user, the first user info is still showing instead of the new one. Note that I am cleaning all my storage when the user hits log out. My code:

Auth validation (guard): I am checking user auth status again after login.

return this.AFauth.authState.pipe(map(auth => {
  if (auth !== null && auth !== undefined) {
    this.saveUserInStorage(auth);
    return true;
  } else {
    this.router.navigate(['/home']);
    return false;
  }
}))

Saving Firebase info in Storage

saveUserInStorage(auth) {
  this.storage.ready().then(() => {
    this.storage.clear(); //cleaning again just in case...
    this.storage.set('user_uid', auth.uid);
    this.storage.set('user_name', auth.displayName);
    this.storage.set('user_email', auth.email);
    this.storage.set('user_photoUrl', auth.photoUrl);
  }).catch(err => {
    console.log('no pudimos guardar');
  });
}

Logout function

logOutUser() {
  firebase.auth().signOut().then(result => {
    // after user hits logout, I erase my storage
    this.storage.remove('user_email');
    this.storage.clear();
    this.router.navigate(['/home']);
  }).catch(error => {
    console.log(error);
    // An error happened.
  });
}

I have to reload my webpage to see the last user logged in.

1 Answers

0
benra On

This might not have anything to do with your storage but rather you have to reset your forms or fields where the information is shown or change the way this information is loaded. I would suggest two options:

  1. On the pages use ionViewWillEnter and put the code in there where the information is pulled out of the storage. (this is probably the easiest)

  2. Use BehaviorSubjects for always emitting a new event when the info is changed and listen to those events where the information is used.

The reason for this is, that every page, once created won't create itself again. You can see this if you console Log something in ngOnInit. Therefore your old information sticks to the page until you find another way to update it. (with ionViewWillEnter or Observables)