All files / src/redux middleware.js

100% Statements 15/15
60% Branches 3/5
100% Functions 6/6
100% Lines 13/13
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40          13x         10x     5x                   13x     5x 3x 2x 2x 2x   1x     5x 2x 2x    
//import { LOGIN_SUCCESS } from "./actionTypes";
 
//const ACCESS_TOKEN = "access_token";
//const EXPIRATION = "expiration";
 
export const middleware = store => next => action => {
  //if (action.type === LOGIN_SUCCESS) {
  //localStorage.setItem(ACCESS_TOKEN, action.payload.auth_token);
  //localStorage.setItem(EXPIRATION, action.payload.expiration);
  //}
  next(action);
};
 
export const initializeState = state => {
  //const token = localStorage.getItem(ACCESS_TOKEN);
  //const expiration = localStorage.getItem(EXPIRATION);
  //if (isAuthenticated(token, expiration)) {
  //  state.user = {
  //    auth_token: token,
  //    refresh_token: null,
  //    expiration: expiration
  //  };
  //}
  return state;
};
 
export const isAuth = state => {
  if (state.user) {
    const token = state.user.auth_token;
    const expiration = state.user.expiration;
    return isAuthenticated(token, expiration);
  }
  return false;
};
 
const isAuthenticated = (token, expiration) => {
  const timenow = new Date().getTime();
  return token && expiration && Number(expiration) > timenow;
};