When refreshing page, the token in the local storage is null.
I am using the latest release of the module and building the authentication layer of my project and I am facing an issue about the token that's generated by the back-end, sent to the front-end and stored in the local store of Redux.
In order, it first logs in, which is performed by the login action, in my auth.js file:
[...]
export function login(username, password) {
return {
[CALL_API]: {
endpoint: '/api/obtain-auth-token/',
method: 'POST',
body: JSON.stringify({ username, password }),
headers: {
'Content-Type': 'application/json',
},
types: [LOGIN, LOGIN_SUCCESS, LOGIN_FAILURE]
}
}
}
[...]
[REHYDRATE]: (state, action) => ({
...state,
token: action.payload.auth ? action.payload.auth.token : null
}),
[...]
[LOGIN_SUCCESS]: (state, action) => ({
...state,
token: action.payload.token
}),
[...]
After executing it, the local storage created by Redux successfully has reduxPersist:auth, holding the token returned from the back-end.
With this process complete, my attempts to perform a route redirection from login page to the dashboard in case the user is logged in, or from any other route to login in case the user is not logged in has faced some issues. These are the index.js files located within routes/[componentName].
// Login and Dashboard had a similar code, just redirecting to a different route.
[...]
onEnter(nextState, replace) {
const { auth: { token } } = store.getState()
console.log(token)
},
[...]
If I log in and move to dashboard, token will have the generated token, but the moment I refresh, it will be NULL, either from login or dashboard pages. Currently I've done a different approach, but I'd like to know if what I was trying to do is possible and/or correct.
I also met this problem, how to resolve it?