{"size": 1033, "ext": "js", "max_stars_repo_path": "app/src/sagas/signIn.js", "max_stars_repo_name": "tom-s/3d-ears", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "app/src/sagas/signIn.js", "max_issues_repo_name": "tom-s/3d-ears", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "app/src/sagas/signIn.js", "max_forks_repo_name": "tom-s/3d-ears", "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": 33.3225806452, "max_line_length": 98, "alphanum_fraction": 0.7047434656}
import { call, put } from 'redux-saga/effects'
import { push } from 'react-router-redux'
import { takeEvery } from 'redux-saga'
import { SIGNIN_REQUEST, SIGNIN_ERROR, SIGNIN_SUCCESS } from '../actions/signIn'
import { signInApi } from '../services/api'
import { setAuthToken } from '../services/auth'
import * as GrowlerActions from '../actions/growler'

export const notifySuccess = () => GrowlerActions.showGrowlerSuccess('You are now logged in')
export const notifyError = () => GrowlerActions.showGrowlerError('Invalid username/password pair')

export function * loginSaga ({ payload }) {
  const { email, password } = payload
  try {
    const { token } = yield call(signInApi, email, password)
    setAuthToken(token)
    yield put({ type: SIGNIN_SUCCESS, payload: token })
    yield put(notifySuccess())
    yield put(push('/'))
  } catch (error) {
    yield put({ type: SIGNIN_ERROR })
    yield put(notifyError())
  }
}

function * watchLogin () {
  yield * takeEvery(SIGNIN_REQUEST, loginSaga)
}

export default watchLogin
