我想将authActions.js中的user_id
变量导出到auth.js中的USER_KEY const这可能吗?
这是我的authActions.js
函数,其中生成user_id
export function loginUser(email, password) {
return function (dispatch) {
return axios.post(SIGNIN_URL, { email, password }).then((response) => {
var { user_id, token } = response.data;
console.log('my user_id = ' + user_id);
console.log('my token = ' + token);
dispatch(authUser(user_id));
isSignedIn();
}).catch((error) => {
console.log(error);
dispatch(addAlert("Could not log in."));
});
};
}
这是我的auth.js
,我有const USER_KEY
import { AsyncStorage } from "react-native";
export const USER_KEY = "auth-key";
console.log('USER_KEY ' + USER_KEY);
export const onSignIn = () => AsyncStorage.setItem(USER_KEY, "true");
export const onSignOut = () => AsyncStorage.removeItem(USER_KEY);
export const isSignedIn = () => {
return new Promise((resolve, reject) => {
AsyncStorage.getItem(USER_KEY)
.then(res => {
if (res !== null) {
resolve(true);
} else {
resolve(false);
}
})
.catch(err => reject(err));
});
};
答案 0 :(得分:0)
变化:
export const onSignIn = () => AsyncStorage.setItem(USER_KEY, "true");
为:
export const onSignIn = (user_id) => AsyncStorage.setItem(USER_KEY, user_id);
然后在POST的.then()
中,你可以这样做:
onSignIn(user_id);
但是之后立即检查isSignedIn()
可能无法按预期工作,因为AsyncStorage
是异步的。如果他们刚刚获得user_id
和token
,是否有必要在那里查看?