<commit_before>import * as constants from '../constants'
import api from '../api'

import cookie from '../utils/cookie'


const saveAuthToken = (token) => {
  const expires = new Date(Date.now() + 7 * 24 * 60 * 60 * 1000)

  cookie.set({
    name: 'token',
    value: token,
    expires,
  })
}

export function login({ email, password, saveToken = true }, callback = null) {
  return (dispatch) => {
    api.user.login({ email, password })
      .then((res) => {
        const data = res.data
        if (saveToken) {
          saveAuthToken(data.tokenId)
        }
        dispatch({
          type: constants.USER_LOGGED_IN,
          payload: data,
        })
        if (callback) {
          callback(data, null)
        }
      })
      .catch((err) => {
        dispatch({
          type: constants.USER_LOGIN_ERROR,
          payload: err.data.error,
        })
        if (callback) {
          callback(null, err)
        }
      })
  }
}

export function logout() {
  cookie.unset('token')
  return {
    type: constants.USER_LOGGED_OUT,
  }
}<commit_msg>Fixed minor big with calling setState before Login component was mounted<commit_after>import * as constants from '../constants'
import api from '../api'

import cookie from '../utils/cookie'


const saveAuthToken = (token) => {
  const expires = new Date(Date.now() + 7 * 24 * 60 * 60 * 1000)

  cookie.set({
    name: 'token',
    value: token,
    expires,
  })
}

export function login({ email, password, saveToken = true }, callback = null) {
  return (dispatch) => {
    api.user.login({ email, password })
      .then((res) => {
        const data = res.data
        if (saveToken) {
          saveAuthToken(data.tokenId)
        }
        if (callback) {
          callback(data, null)
        }
        dispatch({
          type: constants.USER_LOGGED_IN,
          payload: data,
        })
      })
      .catch((err) => {
        if (callback) {
          callback(null, err)
        }
        dispatch({
          type: constants.USER_LOGIN_ERROR,
          payload: err.data.error,
        })
      })
  }
}

export function logout() {
  cookie.unset('token')
  return {
    type: constants.USER_LOGGED_OUT,
  }
}
{'fraction_non_alphanumeric': 0.13588691290469676, 'fraction_numerical': 0.010031919744642043, 'mean_word_length': 1.960863697705803, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '2927540', 'n_tokens_mistral': 698, 'n_tokens_neox': 670, 'n_words': 166}