authReducer.js 706 B

123456789101112131415161718192021222324
  1. export function authReducer(state, action){
  2. if(state === undefined){
  3. if(localStorage.authToken || localStorage.authToken === 'null'){
  4. action.type = 'LOGIN'
  5. action.token = localStorage.authToken
  6. }
  7. else {
  8. return {}
  9. }
  10. }
  11. if(action.type === 'LOGIN'){
  12. console.log('LOGIN')
  13. localStorage.authToken = action.token
  14. let tok = localStorage.authToken.split('.')[1]
  15. let decoded = JSON.parse(atob((tok)))
  16. return {token:action.token, payload: decoded}
  17. }
  18. if(action.type === 'LOGOUT'){
  19. console.log("LOGOUT")
  20. localStorage.authToken = ''
  21. return {}
  22. }
  23. return state
  24. }