auth.js 614 B

1234567891011121314151617181920212223242526
  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. if (token) {
  13. localStorage.authToken = token
  14. return { token, payload: JSON.parse(atob(token.split(".")[1])) }
  15. }
  16. }
  17. if (type === "LOGOUT") {
  18. localStorage.removeItem("authToken")
  19. return {}
  20. }
  21. return state
  22. }
  23. export default authReducer