{"size": 2389, "ext": "js", "max_stars_repo_path": "src/store/sagas/auth.js", "max_stars_repo_name": "afozbek/Burger-Builder", "max_stars_repo_stars_event_min_datetime": "2019-07-31T06:18:49.000Z", "max_stars_repo_stars_event_max_datetime": "2019-07-31T06:18:49.000Z", "max_issues_repo_path": "src/store/sagas/auth.js", "max_issues_repo_name": "afozbek/Burger-Builder", "max_issues_repo_issues_event_min_datetime": "2021-03-09T16:51:22.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-26T17:28:00.000Z", "max_forks_repo_path": "src/store/sagas/auth.js", "max_forks_repo_name": "afozbek/burger-hunger", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": 5, "max_forks_count": null, "avg_line_length": 29.8625, "max_line_length": 136, "alphanum_fraction": 0.6802009209}
import { delay } from "redux-saga/effects";
import { put, call } from "redux-saga/effects";
import axios from "axios";

import * as actions from "../actions";

const SIGNUP_URL = `https://identitytoolkit.googleapis.com/v1/accounts:signUp?key=${process.env.REACT_APP_FIREBASE_API_KEY}`;
const LOGIN_URL = `https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key=${process.env.REACT_APP_FIREBASE_API_KEY}`;

export function* logoutSaga(action) {
  yield call([localStorage, "removeItem"], "token");
  yield call([localStorage, "removeItem"], "expirationDate");
  yield call([localStorage, "removeItem"], "userId");
  yield put(actions.logoutSuccess());
}

export function* checkAuthTimeoutSaga(action) {
  yield delay(action.payload.expirationTime * 1000);
  yield put(actions.logout());
}

export function* authSaga(action) {
  yield put(actions.authStart());

  const authData = {
    email: action.payload.email,
    password: action.payload.password,
    returnSecureToken: true
  };

  let url = `${SIGNUP_URL}`;

  if (!action.payload.isSignup) {
    url = `${LOGIN_URL}`;
  }
  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));
    yield put(actions.checkAuthTimeout(response.data.expiresIn));
  } catch (err) {
    yield put(actions.authFail(err.response.data.error));
  }
}

export function* authCheckStateSaga(action) {
  const token = yield localStorage.getItem("token");
  if (!token) {
    yield put(actions.logout());
  } else {
    const expirationDate = yield new Date(
      localStorage.getItem("expirationDate")
    );
    // Check to see if token is expires
    if (expirationDate <= new Date()) {
      yield put(actions.logout());
    } else {
      const userId = yield localStorage.getItem("userId");
      const authData = {
        localId: userId,
        idToken: token
      };
      yield put(actions.authSuccess(authData));
      yield put(
        actions.checkAuthTimeout(
          (expirationDate.getTime() - new Date().getTime()) / 1000
        )
      );
    }
  }
}
