%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /home/emergentqj/actioncivile/pre-auth/def/static/js/sagas/
Upload File :
Create Path :
Current File : /home/emergentqj/actioncivile/pre-auth/def/static/js/sagas/resetPassword.js

import { put, call, takeEvery } from "redux-saga/effects";
import { initialize } from "redux-form";
import configFunc from "config/config";
import i18n from "i18next";
import LIST_ACTIONS from "../constants/action_types";
import API_RESPONSE_CODES from "../constants/backend_messages";
import { postDataAction } from "../common/actions";
import {
  selectCode, showAlert,
} from "../actions/list_actions";
import { getUniversalCookies } from "../utils/cookies";
import history from "../utils/history";

const config = configFunc();

function* runResetPassword(action) {
  const resetPasswordUrl = `${config.apiUrl}login/changePassword?language=${action.data.language}&country=${action.data.country}&realm=${action.data.realm}`;
  const userName = action.data.username;

  try {
    const payload = yield call(
      postDataAction(resetPasswordUrl, action.data, false, {
        "Content-Type": "application/json",
      }),
    );

    yield put({
      type: action.data.fullToken
        ? LIST_ACTIONS.RESET_PASSWORD_TOKEN_SUCCESS
        : LIST_ACTIONS.RESET_PASSWORD_SUCCESS,
      payload,
    });

    getUniversalCookies().remove("token");
    getUniversalCookies().remove("isLoggedIn");
    getUniversalCookies().remove("session_key");
    getUniversalCookies().remove("refresh_token");
    getUniversalCookies().remove("epSsAuthToken");

    yield put(selectCode(""));
    yield put(
      initialize("signinform", {
        phoneNumber: userName,
      }),
    );
  } catch (e) {
    const data = e.response ? e.response.data : e.data;
    yield put({
      type: action.data.fullToken
        ? LIST_ACTIONS.RESET_PASSWORD_TOKEN_ERROR
        : LIST_ACTIONS.RESET_PASSWORD_ERROR,
      data,
    });
  }
}

function* runCheckToken(action) {
  const resetPasswordUrl = `${config.apiUrl}login/changePassword?language=${action.data.language}&country=${action.data.country}&realm=${action.data.realm}`;
  const redirectToRoot = () => {
    history.push({
      pathname: "/",
      search: "",
    });
    window.location.reload();
  };

  try {
    const params = {
      fullToken: action.data.token,
      newPassword: " ",
    };

    yield call(
      postDataAction(resetPasswordUrl, params, false, {
        "Content-Type": "application/json",
      }),
    );
  } catch (error) {
    if (error.response.data.message.startsWith(API_RESPONSE_CODES.PASSWORD_RESET_TOKEN_INVALID)) {
      yield put(
        showAlert({
          icon: "error",
          show: true,
          title: i18n.t("sweet-alert.sorry"),
          text: "Invalid token",
          didClose: redirectToRoot,
        }),
      );
    }
    if (
      error.response.data.message.startsWith(API_RESPONSE_CODES.PASSWORD_RESET_TOKEN_EXPIRED)
    ) {
      yield put(
        showAlert({
          icon: "error",
          show: true,
          title: i18n.t("sweet-alert.sorry"),
          text: "Expired token",
          didClose: redirectToRoot,
        }),
      );
    }
  }
}

export default function* resetPasswordToken() {
  yield takeEvery(LIST_ACTIONS.RESET_PASSWORD_TOKEN_REQUEST, runResetPassword);
  yield takeEvery(LIST_ACTIONS.RESET_PASSWORD_REQUEST, runResetPassword);
  yield takeEvery(LIST_ACTIONS.CHECK_PASSWORD_RESET_TOKEN, runCheckToken);
}

Zerion Mini Shell 1.0