React Native - Firebase 身份验证持久性不起作用 [英] React Native - Firebase auth persistence not working
问题描述
Firebase 身份验证不会持续登录用户,每次刷新或重新打开应用时,我都必须再次登录.
Firebase auth does not persist logged in user and everytime I refresh or reopen app I have to sign in again.
我尝试将持久性设置为本地,回调确实验证了它的设置,但持久性仍然无效
I have tried setting persistence to local and the callback does verify its set but the persistence is still no working
为了设置持久性,我正在使用...
For setting persistence I am using...
//set auth persistence
firebase.auth().setPersistence(firebase.auth.Auth.Persistence.LOCAL)
.then(function() {
console.log("successfully set the persistence");
})
.catch(function(error){
console.log("failed to ser persistence: " + error.message)
});
...为了登录,我正在使用此代码
. . . For signing in I am using this code
firebase.auth().signInWithEmailAndPassword(email, password)
.then((user) =>{
this.checkAccountStatus(user.uid, user.email);
})
.catch(function(error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
console.log(errorMessage)
// ...
});
这是我用来检查登录状态的代码...
And here is the code I am using to check login status...
if (firebase.auth().currentUser) {
const currentUser = firebase.auth().currentUser;
console.log("Signed in username" + currentUser.displayName);
this.props.navigation.navigate('AppTab');
}else{
console.log("no user signed in");
this.props.navigation.navigate('AuthTab');
}
如果我做错了什么
推荐答案
您不需要设置持久性.默认情况下,Firebase 会为您处理.你只需要调用这个函数来检查用户是否登录:
You don't need to set persistence. Firebase handles it for you by default. You just need to call this function to check whether user is logged or not:
firebase.auth().onAuthStateChanged((user) => {
if (user) {
console.log('user is logged');
}
});
只有在用户退出或清理应用数据时才会触发此操作.
This will not be triggered only if user has sign out or cleaned app data.
您可以在官方文档中找到更多详细信息:https://firebase.google.com/docs/auth/web/manage-users
You can find more details in the official docs: https://firebase.google.com/docs/auth/web/manage-users
希望有帮助.
这篇关于React Native - Firebase 身份验证持久性不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!