Created
July 23, 2019 08:33
-
-
Save kjmczk/64f302546e4cd38ff922cf5d59192b8e to your computer and use it in GitHub Desktop.
#16 User Auth with Knox - Blog
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// frontend/src/reducers/auth.js | |
import { | |
USER_LOADING, | |
USER_LOADED, | |
AUTH_ERROR, | |
LOGIN_SUCCESS, | |
LOGIN_FAIL | |
} from '../actions/types'; | |
const initialState = { | |
isLoading: false, | |
isAuthenticated: null, | |
user: null, | |
token: localStorage.getItem('token') | |
}; | |
export default function(state = initialState, action) { | |
switch (action.type) { | |
case USER_LOADING: | |
return { | |
...state, | |
isLoading: true | |
}; | |
case USER_LOADED: | |
return { | |
...state, | |
isLoading: false, | |
isAuthenticated: true, | |
user: action.payload | |
}; | |
case LOGIN_SUCCESS: | |
localStorage.setItem('token', action.payload.token); | |
return { | |
...state, | |
isLoading: false, | |
isAuthenticated: true, | |
...action.payload | |
}; | |
case AUTH_ERROR: | |
case LOGIN_FAIL: | |
localStorage.removeItem('token'); | |
return { | |
...state, | |
isLoading: false, | |
isAuthenticated: false, | |
user: null, | |
token: null | |
}; | |
default: | |
return state; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment