index.js 906 B

1234567891011121314151617181920212223242526
  1. import { put, call } from "redux-saga/effects";
  2. import * as actions from './../../../actions/auth/signIn'
  3. import axios from 'axios';
  4. import token from './../../../utils/token';
  5. import { SIGN_IN_URL } from './../../../constants/index'
  6. // worker-saga for signing in
  7. export default function* ({ payload: { login, password } }) {
  8. try {
  9. const payload = yield call(() =>
  10. axios.get(`${SIGN_IN_URL}login=${login}&password=${password}`)
  11. .then(({ data: [payload] }) => payload)
  12. );
  13. yield call(() => { if (!payload) throw (new Error("No such email or password")) })
  14. yield put(actions.signInRequestSucces(payload));
  15. yield call(() => localStorage.setItem(token, JSON.stringify(payload)));
  16. }
  17. catch ({ message }) {
  18. yield put(actions.signInRequestFailure(message))
  19. yield call(() => localStorage.removeItem(token));
  20. }
  21. }