要在React Native Firebase上启用持久性,您需要确保已经安装了@react-native-firebase/app
和@react-native-firebase/auth
包,并且正确地配置了Firebase
@react-native-firebase/app
和@react-native-firebase/auth
包(如果尚未安装):npm install @react-native-firebase/app @react-native-firebase/auth
android/settings.gradle
和ios/Podfile
文件中包含了Firebase的配置。import firebase from '@react-native-firebase/app';
import auth from '@react-native-firebase/auth';
const firebaseConfig = {
apiKey: "YOUR_API_KEY",
authDomain: "YOUR_AUTH_DOMAIN",
projectId: "YOUR_PROJECT_ID",
storageBucket: "YOUR_STORAGE_BUCKET",
messagingSenderId: "YOUR_MESSAGING_SENDER_ID",
appId: "YOUR_APP_ID",
};
if (!firebase.apps.length) {
firebase.initializeApp(firebaseConfig);
}
auth().signInWithEmailAndPassword()
方法,并在用户登录后调用setPersistence()
方法来启用持久性:import auth from '@react-native-firebase/auth';
const signIn = async (email, password) => {
try {
await auth().signInWithEmailAndPassword(email, password);
auth().setPersistence(auth.Auth.Persistence.LOCAL); // 启用持久性
} catch (error) {
console.error('Error signing in:', error);
}
};
现在,您的React Native Firebase应用程序将使用本地持久性来存储用户的登录状态,这样即使应用程序关闭并重新打开,用户的登录状态也会保持。
领取专属 10元无门槛券
手把手带您无忧上云