%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /home/emergentqj/actioncivile/pre-auth/.632e77b3fb93bc8da5b589b869bc0037/static/js/sagas/
Upload File :
Create Path :
Current File : /home/emergentqj/actioncivile/pre-auth/.632e77b3fb93bc8da5b589b869bc0037/static/js/sagas/userLogin.js

import {
  put,
  call,
  takeEvery,
} from "redux-saga/effects";
import i18n from "i18next";
import configFunc from "config/config";
import { isLocalhost, trackMixpanelActions } from "utils";
import MIXPANEL_ACTIONS from "constants/mixpanel_actions";
import LIST_ACTIONS from "constants/action_types";

import { postDataAction2 } from "../common/actions";
import { showAlert } from "../actions/list_actions";
import history from "../utils/history";
import { cookies } from "../utils/cookies";

const { businessShort, desktopShort } = configFunc();

// Depending on the user type
const getLocalhostRedirectBase = (payload) => (payload.data.user?.customerType === "PRIVATE" ? desktopShort : businessShort);

// In localhost the base changes as well, not only the path
const getRedirectBase = (payload) => (isLocalhost() ? getLocalhostRedirectBase(payload) : "");

const config = configFunc();

function* runGetUserLogin(dispatch, action) {
  const authData = {};

  if (action.credentials.sso_code) {
    cookies.setCookie("sso_redirect_url", action.credentials.redirectUrl);
    authData.sso_code = action.credentials.sso_code;
  } else {
    const { phoneNumber, password } = action.credentials;
    authData.userName = phoneNumber;
    authData.password = password;
  }

  const loginUserUrl = `${config.apiUrl}login/auth`;

  try {
    const payload = yield call(postDataAction2({
      url: loginUserUrl,
      params: authData,
      auth: true,
      options: {
        // We can get a fully controlled error 401,
        // that we are to handle here,
        // because it is, you know, a login page
        validateStatus: (status) => status < 300 || status === 401,
      },
    }));
    const props = {
      userName: authData.userName,
      sso: false,
    };
    if (payload.data && payload.data.accesses) {
      props.id = payload.data.accesses.currentLoginId;
      props.accountType = payload.data.accesses.accountType;
      props.customerType = payload.data.accesses.groupedAccesses.length > 0
        ? "DUAL"
        : payload.data.accesses.customerType;
      // eslint-disable-next-line prefer-destructuring
      props.CUId = payload.data.accesses.customerIds[0];
      props.countPU = payload.data.accesses.parkingUserIds.length;
      props.countBA = payload.data.accesses.billingAccountIds.length;
    }
    if (payload.data.unauthorized) {
      yield put({
        type: LIST_ACTIONS.USER_AUTH_UNAUTHORIZED,
        payload: payload.data,
      });
      trackMixpanelActions(MIXPANEL_ACTIONS.SIGN_IN_FAILURE, props);
    } else if (action.credentials.sso_code) {
      trackMixpanelActions(MIXPANEL_ACTIONS.SIGN_IN_SUCCESS, props);
      history.push(action.credentials.redirectUrl);
    } else {
      trackMixpanelActions(MIXPANEL_ACTIONS.SIGN_IN_SUCCESS, props);
      const url = new URL(document.location);
      const redirect = url.searchParams.get("redirect_url");
      if (redirect) {
        window.location.href = redirect;
      } else {
        window.location.href = `${getRedirectBase(payload)}${payload.data.redirect}`;
      }
    }
  } catch (e) {
    yield put({
      type: LIST_ACTIONS.USER_AUTH_ERROR,
      e,
    });

    yield put(
      showAlert({
        icon: "error",
        show: true,
        title: i18n.t("sweet-alert.sorry"),
        text: e.unauthorized
          ? i18n.t("wrong-login-or-pass")
          : i18n.t("sweet-alert.try-again"),
      }),
    );
  }
}

export default function* getUserLogin(dispatch) {
  yield takeEvery(LIST_ACTIONS.USER_AUTH_REQUEST, runGetUserLogin, dispatch);
}

Zerion Mini Shell 1.0