I want to export my user_id
variable from authActions.js to USER_KEY const from auth.js Is that possible?
here is my authActions.js
function where generates 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."));
});
};
}
and here is my auth.js
where I have the 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));
});
};
Change:
export const onSignIn = () => AsyncStorage.setItem(USER_KEY, "true");
to:
export const onSignIn = (user_id) => AsyncStorage.setItem(USER_KEY, user_id);
And then in the .then()
of your POST, you can do:
onSignIn(user_id);
But then checking isSignedIn()
immediately after probably won't work as expected, since AsyncStorage
is asynchronous. If they have just gotten a user_id
and token
, is it necessary to check right there?