1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- import thunk from 'redux-thunk';
- import { actionAuthLogin } from '../actions'
- import {createStore, combineReducers, applyMiddleware} from 'redux';
- function promiseReducer(state = {}, { type, name, status, payload, error }) {
- if (type === 'PROMISE') {
- return {
- ...state,
- [name]: { status, payload, error },
- }
- }
- return state
- }
-
- const jwtDecode = (token) => {
- try {
- return JSON.parse(atob(token.split('.')[1]))
- } catch (e) {
- return null
- }
- }
- function authReducer(state, { type, token }) {
- if (state === undefined && localStorage.authToken) {
- token = localStorage.authToken
- type = 'AUTH_LOGIN'
- }
- if (type === 'AUTH_LOGIN') {
- if (jwtDecode(token)) {
- localStorage.authToken = token
- return { token, payload: jwtDecode(token) }
- }
- }
- if (type === 'AUTH_LOGOUT') {
- localStorage.authToken = ''
- return {}
- }
- return state || {}
- }
- export const store = createStore(
- combineReducers({
- promise: promiseReducer,
- auth: authReducer,
- }),
- applyMiddleware(thunk),
- )
|