{"size": 1082, "ext": "js", "max_stars_repo_path": "app/containers/LogInHeader/sagas.js", "max_stars_repo_name": "jasoncyu/progress-pics", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "app/containers/LogInHeader/sagas.js", "max_issues_repo_name": "jasoncyu/progress-pics", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "app/containers/LogInHeader/sagas.js", "max_forks_repo_name": "jasoncyu/progress-pics", "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": 18.6551724138, "max_line_length": 67, "alphanum_fraction": 0.6266173752}
import { take, call, put, select, fork } from 'redux-saga/effects';
import { takeLatest } from 'redux-saga';

import request from 'utils/request';

import {
  LOGOUT,
} from '../Register/constants'

import {
  logoutSuccessAction,
  logoutErrorAction,
} from '../Register/actions'

export function* checkAuth() {
  const res = yield call(request, '/users/get-current-user', {
    method: 'POST',
  })

  if (!res.err) {
    yield put({
      type: 'CHECK_AUTH_SUCCESS',
      payload: {
        user: res.user,
      },
    })
  } else {
    yield put({
      type: 'CHECK_AUTH_ERROR',
      err: res.err,
    })
  }
}

export function* logout() {
  const logoutData = (
    yield call(request, '/users/logout', {
      method: 'POST',
    }))

  if (!logoutData.err) {
    yield put(logoutSuccessAction())
  } else {
    yield put(logoutErrorAction())
  }
}

// Individual exports for testing
export function* defaultSaga() {
  yield fork(takeLatest, 'CHECK_AUTH', checkAuth)
  yield fork(takeLatest, LOGOUT, logout)
}

// All sagas to be loaded
export default [
  defaultSaga,
];
