{"size": 923, "ext": "js", "max_stars_repo_path": "src/store/modules/auth/reducer.js", "max_stars_repo_name": "felipebergamin/gostack10-fastfeed-recatjs", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/store/modules/auth/reducer.js", "max_issues_repo_name": "felipebergamin/gostack10-fastfeed-recatjs", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/store/modules/auth/reducer.js", "max_forks_repo_name": "felipebergamin/gostack10-fastfeed-recatjs", "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": 26.3714285714, "max_line_length": 92, "alphanum_fraction": 0.6208017335}
import produce from 'immer';

import api from '~/services/api';

const INITIAL_STATE = {
  isAuthenticated: false,
  token: null,
};

export default function auth(state = INITIAL_STATE, action = {}) {
  return produce(state, (draft) => {
    switch (action.type) {
      case '@auth/authenticate':
        draft.isAuthenticated = true;
        draft.token = action.payload.token;

        api.defaults.headers.common.Authorization = `Bearer ${action.payload.token}`;
        break;
      case 'persist/REHYDRATE':
        if (action.payload) {
          draft.isAuthenticated = action.payload.auth.isAuthenticated;
          draft.token = action.payload.auth.token;

          api.defaults.headers.common.Authorization = `Bearer ${action.payload.auth.token}`;
        }
        break;
      case '@auth/logout':
        draft.isAuthenticated = false;
        draft.token = null;
        break;
      default:
    }
  });
}
