text: import { delay, put } from 'redux-saga/effects';
  import axios from 'axios';
  
  import * as actions from '../actions';
  
  export function* logoutSaga(action) {
    yield localStorage.removeItem('token');
    yield localStorage.removeItem('expirationDate');
    yield localStorage.removeItem('userId');
    yield put(actions.authLogoutSucceed());
  }
  
  export function* checkAuthTimeoutSaga(action) {
    yield delay(action.expirationTime * 1000);
    yield put(actions.authLogout());
  }
  
  export function* authUserSaga(action) {
    yield put(actions.authStart());
    const authData = {
      email: action.email,
      password: action.password,
      returnSecureToken: true
    }
    let url = 'https://identitytoolkit.googleapis.com/v1/accounts:signUp?key=AIzaSyB-DB4_5VXkSgLksbAKecX-hMH8bsVtAaU';
    if (!action.isSignup) {
      url = 'https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key=AIzaSyB-DB4_5VXkSgLksbAKecX-hMH8bsVtAaU';
    }
    try {
      const response = yield axios.post(url, authData);
      const expirationDate = yield new Date(new Date().getTime() + response.data.expiresIn * 1000);
      yield localStorage.setItem('token', response.data.idToken);
      yield localStorage.setItem('expirationDate', expirationDate);
      yield localStorage.setItem('userId', response.data.localId);
      yield put(actions.authSuccess(response.data.idToken, response.data.localId));
      yield put(actions.checkAuthTimeout(response.data.expiresIn));
    }
    catch (error) {
      yield put(actions.authFail(error.response.data.error));
    }
  }
  
  export function* authCheckStateSaga(action) {
    const token = yield localStorage.getItem('token');
    if (!token) {
      yield put(actions.authLogout());
    } else {
      const expirationDate = yield new Date(localStorage.getItem('expirationDate'));
      if (expirationDate <= new Date()) {
        yield put(actions.authLogout());
      } else {
        yield put(actions.authSuccess(token, localStorage.getItem('userId')));
        yield put(actions.checkAuthTimeout((expirationDate.getTime() - new Date().getTime()) / 1000));
      }
    }
  }