| dispatch(clearLoginErrors()); | dispatch(clearLoginErrors()); | ||||
| dispatch( | dispatch( | ||||
| fetchUser({ | fetchUser({ | ||||
| email, | |||||
| identifier: email, | |||||
| password, | password, | ||||
| handleApiResponseSuccess, | handleApiResponseSuccess, | ||||
| }) | }) |
| }, | }, | ||||
| authentications: { | authentications: { | ||||
| getUsernames: 'authenticate/usernames', | getUsernames: 'authenticate/usernames', | ||||
| login: 'auth/token', | |||||
| login: 'api/auth/local', | |||||
| register: 'api/auth/local/register', | |||||
| getUserSecurityQuestion: 'users/username/securityquestion', | getUserSecurityQuestion: 'users/username/securityquestion', | ||||
| confirmSecurityQuestion: 'authenticate/confirm', | confirmSecurityQuestion: 'authenticate/confirm', | ||||
| confirmForgotPassword: 'users/passwords/reset_token', | confirmForgotPassword: 'users/passwords/reset_token', | ||||
| resetPassword: 'users/passwords', | resetPassword: 'users/passwords', | ||||
| refreshToken: 'auth/refresh', | |||||
| refreshToken: 'api/token/refresh', | |||||
| generateToken: '/authenticate/generate', | generateToken: '/authenticate/generate', | ||||
| authenticate: | authenticate: | ||||
| '/authenticate?fp={fp}&offer={offer}&landingPageUrl={landingPageUrl}®istrationFlowType={registrationFlowType}', | '/authenticate?fp={fp}&offer={offer}&landingPageUrl={landingPageUrl}®istrationFlowType={registrationFlowType}', | ||||
| getRegistrationAccounts: '/users/{userUid}/accounts', | getRegistrationAccounts: '/users/{userUid}/accounts', | ||||
| updateUser: '/users/{userUid}?updateUserActionType={actionType}', | updateUser: '/users/{userUid}?updateUserActionType={actionType}', | ||||
| updateUserPassword: '/users/{userUid}/passwords', | updateUserPassword: '/users/{userUid}/passwords', | ||||
| logout: 'auth/logout', | |||||
| logout: 'api/auth/logout', | |||||
| getUsernames: '/users/email', | getUsernames: '/users/email', | ||||
| createUser: | createUser: | ||||
| '/users?fp={fp}&offer={offer}&landingPageUrl={landingPageUrl}®istrationFlowType={registrationFlowType}', | '/users?fp={fp}&offer={offer}&landingPageUrl={landingPageUrl}®istrationFlowType={registrationFlowType}', |
| import axios from 'axios'; | |||||
| import axios from "axios"; | |||||
| // import queryString from 'qs'; | // import queryString from 'qs'; | ||||
| const request = axios.create({ | const request = axios.create({ | ||||
| baseURL: process.env.REACT_APP_BASE_API_URL, | |||||
| baseURL: "http://localhost:1337/", | |||||
| headers: { | headers: { | ||||
| 'Content-Type': 'application/json', | |||||
| "Content-Type": "application/json", | |||||
| }, | }, | ||||
| // withCredentials: true, | |||||
| withCredentials: true, | |||||
| // paramsSerializer: (params) => | // paramsSerializer: (params) => | ||||
| // queryString.stringify(params, { arrayFormat: 'comma' }), | // queryString.stringify(params, { arrayFormat: 'comma' }), | ||||
| }); | }); | ||||
| request.delete(url, { params, ...options }); | request.delete(url, { params, ...options }); | ||||
| export const downloadRequest = (url, params = null, options = null) => | export const downloadRequest = (url, params = null, options = null) => | ||||
| request.get(url, { params, ...options, responseType: 'blob' }); | |||||
| request.get(url, { params, ...options, responseType: "blob" }); | |||||
| export const replaceInUrl = (url, pathVariables = {}) => { | export const replaceInUrl = (url, pathVariables = {}) => { | ||||
| const keys = Object.keys(pathVariables); | const keys = Object.keys(pathVariables); | ||||
| return keys.reduce( | return keys.reduce( | ||||
| (acc, key) => acc.replace(`{${key}}`, pathVariables[`${key}`]), | (acc, key) => acc.replace(`{${key}}`, pathVariables[`${key}`]), | ||||
| url, | |||||
| url | |||||
| ); | ); | ||||
| }; | }; | ||||
| export const refreshTokenRequest = (payload) => | export const refreshTokenRequest = (payload) => | ||||
| postRequest(apiEndpoints.authentications.refreshToken, payload); | postRequest(apiEndpoints.authentications.refreshToken, payload); | ||||
| export const logoutUserRequest = (payload) => | |||||
| postRequest(apiEndpoints.users.logout, payload); | |||||
| export const logoutUserRequest = () => | |||||
| getRequest(apiEndpoints.users.logout); | |||||
| export const generateTokenRequest = (payload) => | export const generateTokenRequest = (payload) => | ||||
| postRequest(apiEndpoints.authentications.generateToken, payload); | postRequest(apiEndpoints.authentications.generateToken, payload); |
| import jwt from "jsonwebtoken"; | import jwt from "jsonwebtoken"; | ||||
| import { JWT_REFRESH_TOKEN, JWT_TOKEN } from "../../constants/localStorage"; | import { JWT_REFRESH_TOKEN, JWT_TOKEN } from "../../constants/localStorage"; | ||||
| import { attachBeforeRequestListener } from "../../request/index"; | import { attachBeforeRequestListener } from "../../request/index"; | ||||
| import { | |||||
| authScopeStringGetHelper, | |||||
| } from "../../util/helpers/authScopeHelpers"; | |||||
| import { authScopeStringGetHelper } from "../../util/helpers/authScopeHelpers"; | |||||
| import { logoutUser, refreshUserToken } from "../actions/login/loginActions"; | import { logoutUser, refreshUserToken } from "../actions/login/loginActions"; | ||||
| import { apiDefaultUrl } from "../../request/index"; | |||||
| import apiEndpoints from "../../request/apiEndpoints"; | |||||
| export const accessTokensMiddlewareInterceptorName = "ACCESS_TOKEN_INTERCEPTOR"; | export const accessTokensMiddlewareInterceptorName = "ACCESS_TOKEN_INTERCEPTOR"; | ||||
| // If access token is expired, refresh access token | // If access token is expired, refresh access token | ||||
| if (new Date() > new Date(jwtTokenDecoded.exp * 1000)) { | if (new Date() > new Date(jwtTokenDecoded.exp * 1000)) { | ||||
| const axiosResponse = await axios.post( | const axiosResponse = await axios.post( | ||||
| `${process.env.REACT_APP_BASE_API_URL}auth/refresh`, | |||||
| `${apiDefaultUrl}${apiEndpoints.authentications.refreshToken}`, | |||||
| { | { | ||||
| token: refresh, | |||||
| refreshToken: refresh, | |||||
| }, | |||||
| { | |||||
| withCredentials: true, | |||||
| headers: { Authorization: `Bearer ${jwtToken}` }, | |||||
| } | } | ||||
| ); | ); | ||||
| const newToken = axiosResponse.data.newAccessToken; | |||||
| const newToken = axiosResponse.data.jwt; | |||||
| response.headers.Authorization = `Bearer ${newToken}`; | response.headers.Authorization = `Bearer ${newToken}`; | ||||
| ...state, | ...state, | ||||
| token: { | token: { | ||||
| ...state.token, | ...state.token, | ||||
| JwtToken: action.payload.token, | |||||
| JwtRefreshToken: action.payload.refresh | |||||
| JwtToken: action.payload.jwt, | |||||
| JwtRefreshToken: action.payload.refreshToken | |||||
| }, | }, | ||||
| }; | }; | ||||
| } | } |
| function* fetchUser({ payload }) { | function* fetchUser({ payload }) { | ||||
| try { | try { | ||||
| const { data } = yield call(attemptLogin, payload); | const { data } = yield call(attemptLogin, payload); | ||||
| if (data?.token) { | |||||
| const user = jwt.decode(data.token); | |||||
| yield call(authScopeSetHelper, JWT_TOKEN, data.token); | |||||
| yield call(authScopeSetHelper, JWT_REFRESH_TOKEN, data.refresh); | |||||
| yield call(addHeaderToken, data.token); | |||||
| if (data?.jwt) { | |||||
| const user = data?.user; | |||||
| yield call(authScopeSetHelper, JWT_TOKEN, data.jwt); | |||||
| yield call(authScopeSetHelper, JWT_REFRESH_TOKEN, data?.refreshToken); | |||||
| yield call(addHeaderToken, data?.jwt); | |||||
| yield put(setUser(user)); | yield put(setUser(user)); | ||||
| } | } | ||||
| yield put(fetchUserSuccess(data)); | yield put(fetchUserSuccess(data)); | ||||
| const token = yield call(authScopeStringGetHelper, JWT_REFRESH_TOKEN); | const token = yield call(authScopeStringGetHelper, JWT_REFRESH_TOKEN); | ||||
| const user = yield call(jwt.decode, token); | const user = yield call(jwt.decode, token); | ||||
| if (user) { | if (user) { | ||||
| let requestData = { token }; | |||||
| yield call(logoutUserRequest, requestData); | |||||
| yield call(logoutUserRequest); | |||||
| } | } | ||||
| } catch (error) { | } catch (error) { | ||||
| console.log(error); // eslint-disable-line | console.log(error); // eslint-disable-line |