Firebase登录后重定向到页面 - Javascript [英] Redirecting to a page after Firebase login - Javascript
问题描述
我正在使用Firebase使用简单的登录/注册模板创建Web应用。我只是想知道如何在成功登录后重定向到不同的页面或以其他方式运行javascript功能代码,我只在登录用户的同一页面上。
谢谢
I am using Firebase to create a web app using their simple login/signup template. I was just wondering how I can redirect to a different page after a successful login or otherwise run a javascript function code which I have on the same page only for logged in users. Thanks
参考:
https://github.com/firebase/quickstart-js
我使用的模板位于auth / email.html
The template I am using is under the auth/email.html
谢谢!
推荐答案
从用户那里获取电子邮件和密码然后传递值到 signInWithEmailAndPassword
,如果发生某些错误,它将打印该消息,如果没有Firebase将成功签署该用户。
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.
要处理登录和注销,请始终使用 onAuthStateChanged()
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.
这篇关于Firebase登录后重定向到页面 - Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!