Daniel issues in example: state is empty keys
dany338 opened this issue · comments
export const getUserMeRequest = token => {
return async (dispatch, state) => {
dispatch(userMe());
try {
console.log('getUserMeRequest . state token', state, token);
const obj = {
token,
};
const data = await UsersServices.apiUser.getUser(obj);
if (typeof data._id !== 'undefined') {
dispatch(userMeSuccess(data));
} else if (typeof data.error !== 'undefined') {
dispatch(userMeFailed(data.error));
}
} catch (error) {
dispatch(userMeFailed(error));
}
};
};
Hi Daniel, I believe you're trying to make a call to your server, get a JWT and store it in your global store and for some reason, the store is not persisting. For the store issue, I'd need more info and more code to be able to help.
Although, it's generally encouraged to store the JWT in the localStorage
rather than the global store (with or without Redux) as it would be much easier for the client-side to manage this token (security purposes). With that being said, you can have a login method like this somewhere in your util file:
function login(username, password) {
axios.post(
'/api/authenticate',
data: { username, password },
{ headers: { 'Content-Type': 'application/json' }
})
.then(response => {
localStorage.setItem(ACCESS_TOKEN, response.accessToken);
// dispatch your success action
})
.catch(error => {
// dispatch your error action
})
}
Cheers