anonym
anonym

Reputation: 27

How to remain signed in with Firebase auth() when redirecting to another page?

After I logged in in index.html, I redirected the page to localhost:8000/userhome.html. However, the auth() status seems like it is no longer signed in. When I try to console.log(firebase.auth().currentUser) in userhome.html, it printed null. I also tried to set persistence to LOCAL when I signed in, it still shows the same thing. What am I doing wrong?

Upvotes: 2

Views: 546

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317392

You should use an auth state observer to determine when the signed-in user object is available. It won't be immediately available when a page first loads, even after a successful sign-in that happened on a previous page. The callback will tell you when it's ready.

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

Upvotes: 1

Related Questions