我有一個相當簡單的React native/Redux應用程序,並且最近添加了Redux與AsyncStorage一起堅持以幫助在重新加載時結轉狀態。使用React Native/Redux和Firebase持續進行身份驗證
但是,我有問題讓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對象不包含密碼(顯然是出於安全原因)
重新加載後,Firebase會話應該保留。 onAuthStateChanged偵聽器是檢測該狀態的正確方法。重新啓動應用程序後,您的Firebase會話不會持續嗎?最近Firebase修復了與反應原生狀態持久性相關的錯誤。確保在測試之前更新到最新版本。 – bojeil
重新加載後,Firebase會話確實存在。我的問題是如何讓React Native通過Firebase憑證重新進行身份驗證。我已經通過持久化身份驗證狀態解決了這個問題,並且在重新打開時檢查用戶是否存在,然後在這種情況下登錄它們。 – phil
從newb道歉,因爲我可能誤解了我的問題。 Firebase不是問題,而是我在應用程序中堅持自己的狀態。 – phil