如何在 firebase/auth js 中保持会话启用?
how to keep session enabled in firebase/auth js?
当我登录或注册时,一切正常。但是,如果我重新加载页面,我必须重新登录。有没有办法解决?我曾尝试自己设置持久性,但它不起作用。
这是我的代码
const iniciarSesion = (e) => {
const auth = getAuth();
setPersistence(auth, browserLocalPersistence)
.then(() => {
signInWithEmailAndPassword(auth, email, password)
.then((userCredential) => {
setVentana("Tienda")
})
.catch((error) => {
console.log(error)
});
})
}
const registar = (e) => {
const auth = getAuth();
createUserWithEmailAndPassword(auth, email, password)
.then((userCredential) => {
setVentana("Tienda")
})
.catch((error) => {
console.error(error)
});
}
您可以使用 redux-persist 因此如果重新加载页面,redux 存储将继续存在。
这份文件我帮你
https://react-redux-firebase.com/docs/integrations/redux-persist.html
使用工具包实施 redux(设置 redux IMO 的最简单方法)- 你可以按照 this tutorial
重要提示是使用 firebase 包中可用的 onAuthStateChanged
方法更新状态,而不是在 sign-in/ sign-up 函数
期间直接更改它
在大多数 browser-based 环境中,Firebase 已经持久存在并在重新加载页面时恢复用户凭据。来自 auth persistence 上的 Firebase 文档:
For a web application, the default behavior is to persist a user's session even after the user closes the browser. This is convenient as the user is not required to continuously sign-in every time the web page is visited on the same device.
因此,如果您的代码在浏览器中运行,则无需调用 setPersistence(auth, browserLocalPersistence)
。但是,您 做 需要的是,当 Firebase 恢复登录用户时,它会在您的代码中检测到。
在重新加载页面时恢复用户需要 Firebase 调用服务器,其中包括检查帐户是否已被禁用。进行此调用时,currentUser
将为 null
。要在用户恢复时收到通知,以及身份验证状态的其他更改,您需要使用身份验证状态侦听器,如 getting the current user:
文档中的第一个代码片段所示
import { getAuth, onAuthStateChanged } from "firebase/auth";
const auth = getAuth();
onAuthStateChanged(auth, (user) => {
if (user) {
// User is signed in, see docs for a list of available properties
// https://firebase.google.com/docs/reference/js/firebase.User
const uid = user.uid;
// ...
} else {
// User is signed out
// ...
}
});
在此代码中,您将调用 setVentana("Tienda")
以导航到正确的 window。
当我登录或注册时,一切正常。但是,如果我重新加载页面,我必须重新登录。有没有办法解决?我曾尝试自己设置持久性,但它不起作用。 这是我的代码
const iniciarSesion = (e) => {
const auth = getAuth();
setPersistence(auth, browserLocalPersistence)
.then(() => {
signInWithEmailAndPassword(auth, email, password)
.then((userCredential) => {
setVentana("Tienda")
})
.catch((error) => {
console.log(error)
});
})
}
const registar = (e) => {
const auth = getAuth();
createUserWithEmailAndPassword(auth, email, password)
.then((userCredential) => {
setVentana("Tienda")
})
.catch((error) => {
console.error(error)
});
}
您可以使用 redux-persist 因此如果重新加载页面,redux 存储将继续存在。 这份文件我帮你 https://react-redux-firebase.com/docs/integrations/redux-persist.html
使用工具包实施 redux(设置 redux IMO 的最简单方法)- 你可以按照 this tutorial
重要提示是使用 firebase 包中可用的 onAuthStateChanged
方法更新状态,而不是在 sign-in/ sign-up 函数
在大多数 browser-based 环境中,Firebase 已经持久存在并在重新加载页面时恢复用户凭据。来自 auth persistence 上的 Firebase 文档:
For a web application, the default behavior is to persist a user's session even after the user closes the browser. This is convenient as the user is not required to continuously sign-in every time the web page is visited on the same device.
因此,如果您的代码在浏览器中运行,则无需调用 setPersistence(auth, browserLocalPersistence)
。但是,您 做 需要的是,当 Firebase 恢复登录用户时,它会在您的代码中检测到。
在重新加载页面时恢复用户需要 Firebase 调用服务器,其中包括检查帐户是否已被禁用。进行此调用时,currentUser
将为 null
。要在用户恢复时收到通知,以及身份验证状态的其他更改,您需要使用身份验证状态侦听器,如 getting the current user:
import { getAuth, onAuthStateChanged } from "firebase/auth";
const auth = getAuth();
onAuthStateChanged(auth, (user) => {
if (user) {
// User is signed in, see docs for a list of available properties
// https://firebase.google.com/docs/reference/js/firebase.User
const uid = user.uid;
// ...
} else {
// User is signed out
// ...
}
});
在此代码中,您将调用 setVentana("Tienda")
以导航到正确的 window。