auth.js 721 B

123456789101112131415161718192021222324252627282930313233
  1. import { decode as atob } from 'base-64'
  2. // функция authReducer
  3. let signatureToken = (token) => JSON.parse(atob(token.split(".")[1]))
  4. function authReducer(state, { type, token }) {
  5. if (state === undefined) {
  6. if (localStorage.authToken) {
  7. type = "LOGIN"
  8. token = localStorage.authToken
  9. } else {
  10. return {}
  11. }
  12. }
  13. if (type === "LOGIN") {
  14. console.log('LOGIN')
  15. localStorage.authToken = token
  16. return {token, payload: signatureToken(token)}
  17. }
  18. if (type === "LOGOUT") {
  19. console.log('LOGOUT')
  20. localStorage.removeItem("authToken")
  21. return {}
  22. }
  23. return state
  24. }
  25. export default authReducer;