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.
我尝试将持久性设置为local,并且回调确实验证了其设置,但持久性仍然无效
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');
}
如果有什么我做不正确的事情
if there anything I am not doing right
推荐答案
您不需要设置持久性.默认情况下,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屋!