Firebase登录后重定向到页面 - Javascript

时间:2016-08-17 03:34:26

标签: javascript firebase firebase-authentication

我正在使用Firebase使用简单的登录/注册模板创建Web应用。我只是想知道如何在成功登录后重定向到不同的页面或以其他方式运行javascript功能代码,我只在登录用户的同一页面上。 感谢

参考: https://github.com/firebase/quickstart-js

我使用的模板位于auth / email.html

谢谢!

2 个答案:

答案 0 :(得分:7)

从用户处获取电子邮件和密码,然后将值传递给signInWithEmailAndPassword,如果发生某些错误,则会打印该消息,否则Firebase将成功为用户签名。

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

});

您还需要一个处理登录和注销状态的侦听器。如果用户已成功登录,您可以在此处重定向。

要处理登录和注销,请始终使用onAuthStateChanged()

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

当有人登录时,user将填充用户详细信息,您可以使用它重定向到另一个页面。

答案 1 :(得分:1)

您可以在JavaScript中指定location,例如location = 'https://google.com'location = '/logged-in-page.html'