auth.js 575 B

123456789101112131415161718192021222324
  1. import { decode as atob } from 'base-64';
  2. function authReducer(state, { type, token }) {
  3. if (state === undefined) {
  4. if (localStorage.authToken) {
  5. type = "LOGIN"
  6. token = localStorage.authToken
  7. } else {
  8. return {}
  9. }
  10. }
  11. if (type === "LOGIN") {
  12. localStorage.authToken = token
  13. return { token, payload: JSON.parse(atob(token.split(".")[1])) }
  14. }
  15. if (type === "LOGOUT") {
  16. localStorage.removeItem("authToken")
  17. return {}
  18. }
  19. return state
  20. }
  21. export default authReducer