英文:
Problems giving Firebase SDK local login persistence with react-native
问题
我已经尝试了文档中提供的所有工具,以及在Stack Overflow中找到的方法,但对于如何创建登录持久性感到困惑。
`setPersistence(auth, localPersistence);`
在我实施时不起作用,以及一些其他变体也不行。有些来源说我需要实施Redux,而另一些说Firebase应该默认具备这一功能,我只需要实施类似于以下的内容:
```javascript
firebase.auth().onAuthStateChanged((user) => {
if (user) {
console.log('用户已登录');
}
});
以下是我当前用于身份验证的代码,请告诉我如何设置本地持久性。
Firebase电话验证函数:
希望这能帮助您解决问题。
<details>
<summary>英文:</summary>
I've tried all the tools given in the documentation and in stack overflow and am confused about how to create login persistence.
`setPersistence(auth, localPersistence); `
does not work when I implement it, as well as some other variations of this. Some sources say that I need to implement redux while some say that firebase should have this as a default and all I need is to implement something like
firebase.auth().onAuthStateChanged((user) => {
if (user) {
console.log('user is logged');
}
});
Here is my code right now for authentication, please let me know how to set local persistence.
Firebase phone auth functions:
'''
const sendVerification = () => {
const phoneProvider = new firebase.auth.PhoneAuthProvider();
phoneProvider
.verifyPhoneNumber(phoneNumberWithCode, recaptchaVerifier.current)
.then(setVerificationId);
setConfirming(!confirming);
setCode('');
};
const confirmCode = () => {
const credential = firebase.auth.PhoneAuthProvider.credential(
verificationId,
code
);
firebase.auth().signInWithCredential(credential)
.then(() => {
setCode('');
checkIfPhoneExists(phoneNumberWithCode).then((userExists) => {
if (userExists) {
navigation.navigate('Threads');
} else {
navigation.navigate('EnterInfo');
}
})
createAccountIfNotUser(phoneNumberWithCode);
}).catch((error) => {
console.log(error);
});
};
Firebase app and auth initialization:
export const app = firebase.initializeApp(firebaseConfig);
export const auth = getAuth(app);
auth.languageCode = 'it';
setPersistence(auth, localPersistence);
</details>
# 答案1
**得分**: 1
你可以使用[@react-native-async-storage/async-storage][1]来实现与Firebase的持久性,然后在firebase应用中使用它。
```javascript
import AsyncStorage from "@react-native-async-storage/async-storage";
import { initializeApp } from "firebase/app";
import { initializeAuth } from "firebase/auth";
import { getReactNativePersistence } from "firebase/auth/react-native";
const app = initializeApp({ ... });
initializeAuth(app, {
persistence: getReactNativePersistence(AsyncStorage),
});
英文:
In order to achieve persistance with Firebase, you can install @react-native-async-storage/async-storage and use it with the firebase app.
import AsyncStorage from "@react-native-async-storage/async-storage";
import { initializeApp } from "firebase/app";
import { initializeAuth } from "firebase/auth";
import { getReactNativePersistence } from "firebase/auth/react-native";
const app = initializeApp({ ... });
initializeAuth(app, {
persistence: getReactNativePersistence(AsyncStorage),
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论