React Native - Firebase 身份验证持久性不起作用
React Native - Firebase auth persistence not working
Firebase 身份验证不会保留已登录的用户,每次刷新或重新打开应用程序时我都必须重新登录。
我已尝试将持久性设置为本地,回调确实验证了它的设置,但持久性仍然无效
为了设置持久性,我正在使用...
//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)
});
。
.
.
为了登录,我使用此代码
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)
// ...
});
这是我用来检查登录状态的代码...
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 会为您处理。你只需要调用这个函数来检查用户是否登录:
firebase.auth().onAuthStateChanged((user) => {
if (user) {
console.log('user is logged');
}
});
仅当用户注销或清除应用数据时才会触发。
您可以在官方文档中找到更多详细信息:https://firebase.google.com/docs/auth/web/manage-users
希望对您有所帮助。
确保您没有使用您正在使用的 API 键限制 console 中的 'Token Service API'。我没有将服务添加到我的密钥中,它每 3-4 小时就会让我退出一次,即使使用上面建议的正确代码也是如此。
Firebase
现在建议使用 firestore
而不是 realtime-database
并且它默认管理离线持久性。它的文档中很清楚 here
您只需通过此代码访问用户:
if (auth().currentUser !== null) {
console.log('User is logged in');
console.log(auth().currentUser.email);
props.navigation.navigate('Home');
} else {
props.navigation.navigate('Login');
}
如果用户已登录,即使您关闭应用程序,此代码也会将您带到主屏幕。要清除用户的凭据,您需要使用此代码手动注销用户。
try {
auth()
.signOut()
.then(props.navigation.navigate('Login'));
} catch (error) {
Alert.alert('Error', error.toString());
}
您还可以查看 this 简单的应用程序(其中使用 react hooks
)以获得进一步的帮助。
Firebase 身份验证不会保留已登录的用户,每次刷新或重新打开应用程序时我都必须重新登录。
我已尝试将持久性设置为本地,回调确实验证了它的设置,但持久性仍然无效
为了设置持久性,我正在使用...
//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)
});
。 . . 为了登录,我使用此代码
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)
// ...
});
这是我用来检查登录状态的代码...
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 会为您处理。你只需要调用这个函数来检查用户是否登录:
firebase.auth().onAuthStateChanged((user) => {
if (user) {
console.log('user is logged');
}
});
仅当用户注销或清除应用数据时才会触发。
您可以在官方文档中找到更多详细信息:https://firebase.google.com/docs/auth/web/manage-users
希望对您有所帮助。
确保您没有使用您正在使用的 API 键限制 console 中的 'Token Service API'。我没有将服务添加到我的密钥中,它每 3-4 小时就会让我退出一次,即使使用上面建议的正确代码也是如此。
Firebase
现在建议使用 firestore
而不是 realtime-database
并且它默认管理离线持久性。它的文档中很清楚 here
您只需通过此代码访问用户:
if (auth().currentUser !== null) {
console.log('User is logged in');
console.log(auth().currentUser.email);
props.navigation.navigate('Home');
} else {
props.navigation.navigate('Login');
}
如果用户已登录,即使您关闭应用程序,此代码也会将您带到主屏幕。要清除用户的凭据,您需要使用此代码手动注销用户。
try {
auth()
.signOut()
.then(props.navigation.navigate('Login'));
} catch (error) {
Alert.alert('Error', error.toString());
}
您还可以查看 this 简单的应用程序(其中使用 react hooks
)以获得进一步的帮助。