gpt4 book ai didi

react-native - 如何检查用户是否已登录,使用 firebase react native ?

转载 作者:行者123 更新时间:2023-12-04 04:50:32 26 4
gpt4 key购买 nike

这是我到目前为止所拥有的,我使用 redux 来全局维护登录状态。在我的登录屏幕中,我正在使用 Action 创建器来像这样登录用户......

    // In LoginScreen file
onLogin = () => {
this.props.accountLogin({ email, password });
}

在操作文件中, accountLogin定义为:
    // In auth_action file
export const accountLogin = ({ email, password }) => {
firebase.auth().signInWithEmailAndPassword(email, password)
.then(user => {
dispatch({ type: LOGIN_SUCCESS, payload: user })});
});
};

然后,在 reducer 文件中,案例 LOGIN_SUCCESS是这样处理的...
    // In auth_reducer file
const INITIAL_STATE = {
// ... Other stuff
user: null
};
export default function (state = INITIAL_STATE, action) {
// ... Other cases
// Login Successful case
case LOGIN_SUCCESS:
return { ...state, user: action.payload };
// After this return statement, I now have user's profile info
};

所以我的问题是,如何保留用户的个人资料信息,以便下次用户打开应用程序时,他们不必重新登录?我做了一些研究,这就是我所拥有的:
    // Use autoRehydrate and persistStore in 'redux-persist'
// In store file
import { createStore, compose, applyMiddleware } from 'redux';
import thunk from 'redux-thunk';
import { autoRehydrate, persistStore } from 'redux-persist';
import { AsyncStorage } from 'react-native';
import reducers from '../reducers';

const store = createStore(
reducers,
{},
compose(
applyMiddleware(thunk),
autoRehydrate()
)
);

persistStore(store, { storage: AsyncStorage, whitelist: ['auth'] });

export default store;

当然,我确实处理了 REHYDRATE我的案例 auth_reducer文件。但是,确实在用户重新打开应用程序后,我拥有该用户上次使用该应用程序时的用户信息,我该如何使用这条信息来继续修改我在 firebase 数据库中的数据?

例如,如果用户重新打开应用程序,并希望更改他的名字,firebase 怎么知道这是一个授权用户?这个怎么写 changeName功能?

最佳答案

这个方法可以帮助你。在你的 componentWillMount 中设置

firebase.auth().onAuthStateChanged((user) => {
if (user) {
console.log('user logged')
}
});

更多信息在这里: https://firebase.google.com/docs/auth/

关于react-native - 如何检查用户是否已登录,使用 firebase react native ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46121928/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com