{"size": 769, "ext": "js", "max_stars_repo_path": "client/redux/actions/authActions.js", "max_stars_repo_name": "Laseen/ee-summer-camp-2018", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "client/redux/actions/authActions.js", "max_issues_repo_name": "Laseen/ee-summer-camp-2018", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "client/redux/actions/authActions.js", "max_forks_repo_name": "Laseen/ee-summer-camp-2018", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 28.4814814815, "max_line_length": 116, "alphanum_fraction": 0.7035110533}
import { API_URL } from '../../config';

export const authActionTypes = {
  SET_AUTH_STATE_FROM_LOCAL_STORAGE: 'SET_AUTH_STATE_FROM_LOCAL_STORAGE',
  SAVE_USER: 'SAVE_USER'
};

export function setAuthStateFromLocalStorage(persistedState) {
  return dispatch => dispatch({ type: authActionTypes.SET_AUTH_STATE_FROM_LOCAL_STORAGE, payload: persistedState });
}

export function fetchUser() {
  return fetch(`${API_URL}/user`, { credentials: 'include' })
    .then(response => {
      if (!response.ok) throw Error(response.statusText);
      return response.json();
    })
    .catch(error => {
      console.error(error);
      return null;
    });
}

export function saveUser(user) {
  return dispatch => dispatch({ type: authActionTypes.SAVE_USER, payload: user });
}
