<commit_before>import {resolve, reject} from 'redux-simple-promise';
import {push} from 'react-router-redux'
import cookie from 'react-cookie';

const LOGIN = 'auth/LOGIN';
const LOGOUT = 'auth/LOGOUT';
const SIGNUP = 'auth/SIGNUP';
const LOAD_USER = 'auth/LOAD_USER';

export default function reducer(state = {}, action = {}) {
  switch (action.type) {
    case LOGOUT:
      cookie.remove('token');

      return Object.assign({}, state, {
        token: null,
        user: null,
        error: null
      });
    case LOGIN:
    case SIGNUP:
      return Object.assign({}, state, {
        token: null,
        user: null,
        error: null
      });
    case resolve(SIGNUP):
    case resolve(LOGIN):
      cookie.save('token', action.payload.data.token);

      return Object.assign({}, state, {
        token: action.payload.data.token,
        user: null,
        error: null
      });
    case reject(LOGIN):
    case reject(SIGNUP):
      return Object.assign({}, state, {
        token: null,
        user: null,
        error: action.payload.data
      });
    case LOAD_USER:
      return Object.assign({}, state, {
        user: null,
        error: null
      });
    case resolve(LOAD_USER):
      return Object.assign({}, state, {
        user: action.payload.data,
        error: null
      });
    case reject(LOAD_USER):
      return Object.assign({}, state, {
        user: null,
        error: action.payload.data
      });
    default:
      return state;
  }
}

export function getToken(globalState) {
  return globalState.auth.token;
}

export function isAuthenticated(globalState) {
  return globalState.auth && globalState.auth.token;
}

export function loadUser() {
  return {
    type: LOAD_USER,
    payload: {
      promise: client => client.get('/api/users/me')
    }
  }
}

export function login({email, password}) {
  const action = {
    type: LOGIN,
    payload: {
      promise: client => client.post('/auth/local', {email, password})
    }
  };

  return dispatch => dispatch(action).then(() => dispatch(push('/')));
}

export function signup({name, email, password}) {
  const action = {
    type: SIGNUP,
    payload: {
      promise: client => client.post('/api/users', {name, email, password})
    }
  };

  return dispatch => dispatch(action).then(() => dispatch(push('/')));
}

export function goToSignup() {
  return push('/signup');
}

export function goToLogin() {
  return push('/login');
}

export function logout() {
  const action = {
    type: LOGOUT
  };

  return dispatch => {
    dispatch(action);
    dispatch(push('/login'));
  };
}
<commit_msg>fix(auth): move to login if invalid token<commit_after>import {resolve, reject} from 'redux-simple-promise';
import {push} from 'react-router-redux'
import cookie from 'react-cookie';

const LOGIN = 'auth/LOGIN';
const LOGOUT = 'auth/LOGOUT';
const SIGNUP = 'auth/SIGNUP';
const LOAD_USER = 'auth/LOAD_USER';

export default function reducer(state = {}, action = {}) {
  switch (action.type) {
    case LOGOUT:
      cookie.remove('token');

      return Object.assign({}, state, {
        token: null,
        user: null,
        error: null
      });
    case LOGIN:
    case SIGNUP:
      return Object.assign({}, state, {
        token: null,
        user: null,
        error: null
      });
    case resolve(SIGNUP):
    case resolve(LOGIN):
      cookie.save('token', action.payload.data.token);

      return Object.assign({}, state, {
        token: action.payload.data.token,
        user: null,
        error: null
      });
    case reject(LOGIN):
    case reject(SIGNUP):
      return Object.assign({}, state, {
        token: null,
        user: null,
        error: action.payload.data
      });
    case LOAD_USER:
      return Object.assign({}, state, {
        user: null,
        error: null
      });
    case resolve(LOAD_USER):
      return Object.assign({}, state, {
        user: action.payload.data,
        error: null
      });
    case reject(LOAD_USER):
      // TODO: change token to null only if error 401
      return Object.assign({}, state, {
        token: null,
        user: null,
        error: action.payload.data
      });
    default:
      return state;
  }
}

export function getToken(globalState) {
  return globalState.auth.token;
}

export function isAuthenticated(globalState) {
  return globalState.auth && globalState.auth.token;
}

export function loadUser() {
  return {
    type: LOAD_USER,
    payload: {
      promise: client => client.get('/api/users/me')
    }
  }
}

export function login({email, password}) {
  const action = {
    type: LOGIN,
    payload: {
      promise: client => client.post('/auth/local', {email, password})
    }
  };

  return dispatch => dispatch(action).then(() => dispatch(push('/')));
}

export function signup({name, email, password}) {
  const action = {
    type: SIGNUP,
    payload: {
      promise: client => client.post('/api/users', {name, email, password})
    }
  };

  return dispatch => dispatch(action).then(() => dispatch(push('/')));
}

export function goToSignup() {
  return push('/signup');
}

export function goToLogin() {
  return push('/login');
}

export function logout() {
  const action = {
    type: LOGOUT
  };

  return dispatch => dispatch(action).then(() => dispatch(push('/login')));
}

{'fraction_non_alphanumeric': 0.1486155584855905, 'fraction_numerical': 0.000565078169146732, 'mean_word_length': 2.6, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5134942', 'n_tokens_mistral': 1630, 'n_tokens_neox': 1630, 'n_words': 407}