【发布时间】:2021-05-06 19:30:04
【问题描述】:
我将 react native 用于 ios 应用程序并使用 firebase 进行身份验证。每次我离开应用程序并返回时,它都会要求登录。我想保留 firebase 登录,但不知道该放在哪里。
我知道我需要把这个放进去:
firebase.auth().setPersistence(firebase.auth.Auth.Persistence.LOCAL)
我有以下登录功能,当在登录屏幕上按下登录按钮时运行:
const signIn = async () => {
setLoading(true);
try {
await firebase.signIn(email, password);
const uid = firebase.getCurrentUser().uid;
const userInfo = await firebase.getUserInfo(uid);
const emailArr = userInfo.email.split("@");
setUser({
username: emailArr[0],
email: userInfo.email,
uid,
isLoggedIn: true,
});
} catch (error) {
alert(error.message);
} finally {
isMounted.current && setLoading(false);
}
};
我的 firebaseContext 中有以下登录内容:
const Firebase = {
getCurrentUser: () => {
return firebase.auth().currentUser;
},
signIn: async (email, password) => {
return firebase.auth().signInWithEmailAndPassword(email, password);
},
getUserInfo: async (uid) => {
try {
const user = await db.collection("users").doc(uid).get();
if (user.exists) {
return user.data();
}
} catch (error) {
console.log("Error @getUserInfo", error);
}
},
logOut: async () => {
return firebase
.auth()
.signOut()
.then(() => {
return true;
})
.catch((error) => {
console.log("Error @logout", error);
});
},
};
我应该把上面列出的文档中的持久化代码放在哪里?
谢谢!
【问题讨论】:
-
默认已经是本地的了,不需要设置-firebase.google.com/docs/auth/web/…
标签: reactjs firebase react-native firebase-authentication