I'm very new to React & Redux and am working on the authentication portion of my app.
The way my backend API works is, once a user signs in, the response contains:
- The JWT token to use for future requests
- An object with the User's data
What I would like to do is have the following occur:
- Store the token in
localstorage
- Set a variable
isAuthenticated
in Redux totrue
(false
for logout) - Store the user information in a second Redux variable called
user
I've currently the following Action:
import { SIGN_IN, SIGN_OUT } from "./Types";
export const signIn = response => {
return {
type: SIGN_IN,
payload: response
};
}
export const signOut = () => {
return {
type: SIGN_OUT
};
}
and the following Reducer:
import { SIGN_IN, SIGN_OUT } from './Types';
const INITIAL_STATE = {
isAuthenticated: null,
user: {}
};
const authReducer = (state = INITIAL_STATE, action) => {
switch(action.type) {
case SIGN_IN:
// This logic was removed thanks to comment from Sanish Joseph
// localStorage.setItem("token", action.payload.token);
return {...state, isAuthenticated: true, user: action.payload.user};
case SIGN_OUT:
// This logic was removed thanks to comment from Sanish Joseph
// localStorage.removeItem("token");
return {...state, isAuthenticated: false, user: {}};
default:
return state;
};
};
export default authReducer;
and CombineReducers code:
export default combineReducers({
...
auth: authReducer
});
This code works, but both user
and isAuthenticated
are children of auth
(in other words, I have to get them both and refer to them via auth.user
& auth.isAuthenticated
.
What I don't know how to do is how to write my reducer code so SIGN_IN
will still send all the data I got from the API, but be able to create 2 separate pieces of state in Redux isAuthenticated
& user
.
Any help would really be appreciated!