我有一个相当简单的React本机/ Redux应用程序,并且最近添加了Redux persist with AsyncStorage以帮助在重新加载时继承状态.
但是,我在让Firebase重新验证用户时遇到问题.我想知道是否有人有经验,因为我对Redux和Firebase都很新.为了更清楚,我希望用户登录一次,然后每次打开应用程序时都不再登录.
export const loginUser = ({ email,password }) => { return (dispatch) => { dispatch({ type: LOGIN_USER }); firebase.auth().signInWithEmailAndPassword(email,password) .then(user => loginUserSuccess(dispatch,user)) .catch(() => { firebase.auth().createUserWithEmailAndPassword(email,password) .then(user => loginUserSuccess(dispatch,user)) .catch(() => loginUserFail(dispatch)); }); }; };
我的App.js:
class App extends Component { constructor() { super(); this.state = { rehydrated: false,store }; } componentDidMount() { const persistor = persistStore( store,{ storage: AsyncStorage,whitelist: ['auth'] },() => { this.setState({ rehydrated: true }); } ); AppState.addEventListener('change',() => this.handleAppLoaded(AppState.currentState)); const firebase_config = { apiKey: Config.FIREBASE_API_KEY,authDomain: `${Config.FIREBASE_PROJECT_NAME}.firebaseapp.com`,databaseURL: `https://${Config.FIREBASE_PROJECT_NAME}.firebaseio.com`,storageBucket: `${Config.FIREBASE_PROJECT_NAME}.appspot.com`,messagingSenderId: Config.FIREBASE_MESSAGE_ID }; firebase.initializeApp(firebase_config); } componentWillUnmount() { AppState.removeEventListener('change',() => this.handleAppLoaded(AppState.currentState)); } handleAppLoaded(state) { if (state === 'active') { store.dispatch(renewToken(store.getState())); } return null; } render() { if (!this.state.rehydrated) return null; this.handleAppLoaded(AppState.currentState); return ( <Provider store={store}> <RouterWithRedux /> </Provider> ); } } export default App;
有人能指出我正确的方向吗?我已经尝试进行reauthUser操作,但由于我找不到重新验证用户的方法而陷入困境,因为Auth对象没有保存密码(显然出于安全原因)
您使用的是网络SDK吗?如果你是,那么首先你需要使用它而不是
https://github.com/invertase/react-native-firebase
原文链接:https://www.f2er.com/react/301063.html这些库用于具有本机支持的react-native项目.
如果您已经在使用它,可以在https://rnfirebase.io/docs/v3.2.x/auth/reference/auth查看API
设置所有onUserChanged,onAuthStateChangedetc.然后你的应用程序应该没有问题重新验证的东西.