gpt4 book ai didi

javascript - Firebase 在网页之间传递身份验证

转载 作者:行者123 更新时间:2023-11-29 21:25:29 24 4
gpt4 key购买 nike

我不知道如何在我的 web.xml 之间共享身份验证。例如,我想在用户登录 login.html 后将身份验证传递给主页。

问题是什么:

用户在login.html 中登录后,创建身份验证。

//login.html
var user = firebase.auth().currentUser;
if (user) {
// User is signed in.
}

但是,我需要在用户登录后将 location.href 更改为 home.html。

//login.html
if (user) {
// User is signed in.
location.href = "home.html";
}

那么,在更改网页时保持认证的解决方案是什么?

  //home.html
if (user) {
//
}else {
// No user is signed in.
}

web/login.html <--> web/home.html

最佳答案

Firebase 通过将 session ID 存储在本地存储中来跟踪页面之间的身份验证状态。

确保您的用户流畅使用的最佳方法是 monitor the authentication state .来自该文档:

The recommended way to get the current user is by setting an observer on the Auth object:

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

By using an observer, you ensure that the Auth object isn't in an intermediate state—such as initialization—when you get the current user.

关于javascript - Firebase 在网页之间传递身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37550498/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com