| 123456789101112131415161718192021222324252627282930313233343536373839404142 |
- import { all, call, put, takeLatest } from "@redux-saga/core/effects";
- import { attemptAuthProvider } from "../../request/loginRequest";
- import {
- fetchUserError,
- fetchUserSuccess,
- } from "../actions/login/loginActions";
- import { setUser } from "../actions/user/userActions";
- import { addHeaderToken } from "../../request";
- import { ACCESS_TOKEN, JWT_REFRESH_TOKEN, JWT_TOKEN } from "../../constants/localStorage";
- import { storeData } from "../../service/asyncStorage";
- import { rejectErrorCodeHelper } from "../../utils/rejectErrorMessageHelper";
- import { AUTH_PROVIDER_FETCH } from "../actions/authProvider/authProviderActionConstants";
- import {
- fetchAuthProviderError,
- fetchAuthProviderSuccess,
- } from "../actions/authProvider/authProviderActions";
-
- function* fetchAuthProvider({ payload }) {
- try {
- const { data } = yield call(attemptAuthProvider, payload.accessToken);
- if (data?.jwt) {
- const user = data?.user;
- yield call(storeData, JWT_TOKEN, data.jwt);
- yield call(storeData, JWT_REFRESH_TOKEN, data?.refreshToken);
- yield call(storeData, ACCESS_TOKEN, payload.accessToken);
- yield call(addHeaderToken, data?.jwt);
- yield put(setUser(user));
- }
- yield put(fetchUserSuccess(data));
- yield put(fetchAuthProviderSuccess("Success"));
- } catch (e) {
- if (e.response && e.response.data) {
- const errorMessage = yield call(rejectErrorCodeHelper, e);
- yield put(fetchUserError(errorMessage));
- yield put(fetchAuthProviderError('Error'));
- }
- }
- }
-
- export default function* authProviderSaga() {
- yield all([takeLatest(AUTH_PROVIDER_FETCH, fetchAuthProvider)]);
- }
|