How to redirect after a user signs in with Firebase authentication?

后端 未结 4 1479
孤独总比滥情好
孤独总比滥情好 2020-12-03 06:35

How can I redirect to a different webpage after the user has signed in?

Currently when a user logs in, data gets retrieved however, it doesn\'t redirect the user to

4条回答
  •  一向
    一向 (楼主)
    2020-12-03 06:55

    Get the email and password from user and then pass the values to signInWithEmailAndPassword, if some error occurs it will print the message, if not Firebase will successfully sign the user in.

    firebase.auth().signInWithEmailAndPassword(email, password).catch(function(error) {
        // Handle Errors here.
        var errorCode = error.code;
        console.log(error.Message);
    
    });
    

    You also need a listener that handles login and logout status. This is where you can redirect users if they have successfully logged in.

    To handle login and logout, always use onAuthStateChanged()

    //Handle Account Status
    firebase.auth().onAuthStateChanged(user => {
      if(user) {
        window.location = 'home.html'; //After successful login, user will be redirected to home.html
      }
    });
    

    The moment someone logins, user will be populated with user details and you can use it to redirect to another page.

提交回复
热议问题