user9847788
user9847788

Reputation: 2445

How to save user data when they log in using firebase authentication?

I am using Firebase Authentication to allow users to log into my Angular app.

Below is the login() method in my AuthService:

login(email: string, password: string) {
   return from(firebase.auth().signInWithEmailAndPassword(email, password));
}

This is working fine, but now I am trying to store the user's data when they log in, so that they're not accidentally logged out when they refresh the app.

Can someone please tell me how I can use the object returned in the above method to ensure a user remains logged in unless they choose to log out of the app?

Upvotes: 0

Views: 1139

Answers (2)

Alan Grosz
Alan Grosz

Reputation: 1315

You have to persist the data. Look this doc:

https://firebase.google.com/docs/auth/web/auth-state-persistence

Upvotes: 0

Peter Haddad
Peter Haddad

Reputation: 80914

When you call firebase.auth().signInWithEmailAndPassword(email, password) then the user will stay logged in. You can check if the user is null or not by using:

firebase.auth().onAuthStateChanged(function(user) {
  if (user) {
    // User is signed in.
  } else {
    // No user is signed in.
  }
});

https://firebase.google.com/docs/auth/web/manage-users

If user is signed in ,then just direct the user to the pages after login.

Upvotes: 2

Related Questions