text: import axios from 'axios';
  import history from '../history';
  
  const TOKEN = 'token';
  
  /**
   * ACTION TYPES
   */
  const SET_AUTH = 'SET_AUTH';
  
  /**
   * ACTION CREATORS
   */
  const setAuth = (auth) => ({ type: SET_AUTH, auth });
  
  /**
   * THUNK CREATORS
   */
  export const me = () => async (dispatch) => {
    const token = window.localStorage.getItem(TOKEN);
    if (token) {
      const res = await axios.get('/auth/me', {
        headers: {
          authorization: token,
        },
      });
      return dispatch(setAuth(res.data));
    }
  };
  
  export const authenticate =
    (username, email, password, method) => async (dispatch) => {
      try {
        const res = await axios.post(`/auth/${method}`, {
          username,
          email,
          password,
        });
        window.localStorage.setItem(TOKEN, res.data.token);
        dispatch(me());
      } catch (authError) {
        return dispatch(setAuth({ error: authError }));
      }
    };
  
  export const updateAccount =
    (username, email, password, userId) => async (dispatch, getState) => {
      try {
        const token = window.localStorage.getItem(TOKEN);
        const { data } = await axios.put(
          `/api/users/${userId}`,
          {
            username,
            email,
            password,
          },
          {
            headers: {
              authorization: token,
            },
          }
        );
        await dispatch(setAuth(data));
      } catch (authError) {
        return dispatch(setAuth({ ...getState().auth, error: authError }));
      }
    };
  
  export const logout = () => {
    window.localStorage.removeItem(TOKEN);
    history.push('/login');
    return {
      type: SET_AUTH,
      auth: {},
    };
  };
  
  /**
   * REDUCER
   */
  export default function (state = {}, action) {
    switch (action.type) {
      case SET_AUTH:
        return action.auth;
      default:
        return state;
    }
  }
  