diff --git a/src/config/authKeys.tsx b/src/config/authKeys.tsx new file mode 100644 index 00000000..a6199593 --- /dev/null +++ b/src/config/authKeys.tsx @@ -0,0 +1,7 @@ +export const AUTH_KEYS = { + authUser: 'AuthUser', + backLocation: 'backLocation', + cookieToken: 'token', + headerToken: 'x-auth-token', + idToken: 'id_token', +} diff --git a/src/config/index.tsx b/src/config/index.tsx index 49800c7a..bf81f61f 100644 --- a/src/config/index.tsx +++ b/src/config/index.tsx @@ -1 +1,3 @@ export * from './routes' +export * from './pages' +export * from './authKeys' diff --git a/src/config/pages.tsx b/src/config/pages.tsx new file mode 100644 index 00000000..f960c655 --- /dev/null +++ b/src/config/pages.tsx @@ -0,0 +1,3 @@ +export const PAGES = { + login: '/login', +} diff --git a/src/helpers/callApi/clearUserAuthInfo.tsx b/src/helpers/callApi/clearUserAuthInfo.tsx index c48925f4..1af1eeed 100644 --- a/src/helpers/callApi/clearUserAuthInfo.tsx +++ b/src/helpers/callApi/clearUserAuthInfo.tsx @@ -1,4 +1,6 @@ +import { AUTH_KEYS } from 'config' + export const clearUserAuthInfo = () => { - localStorage.removeItem('id_token') - localStorage.removeItem('AuthUser') + localStorage.removeItem(AUTH_KEYS.idToken) + localStorage.removeItem(AUTH_KEYS.authUser) } diff --git a/src/helpers/callApi/getRequestConfig.tsx b/src/helpers/callApi/getRequestConfig.tsx index 8bbf113e..e9eb7f3f 100644 --- a/src/helpers/callApi/getRequestConfig.tsx +++ b/src/helpers/callApi/getRequestConfig.tsx @@ -1,34 +1,28 @@ -import isString from 'lodash/isString' +import { AUTH_KEYS } from 'config' import { loadIdToken } from './loadIdToken' -import { TRequestAbortController, TRequestConfig } from './types' +import { TRequestConfig } from './types' export const getRequestConfig = ( config: TRequestConfig, - abortController?: TRequestAbortController, + signal?: AbortSignal, ) => { const requestConfig = { - method: 'POST', ...config, - headers: new Headers(), - } - - if (config.body && !isString(config.body)) { - requestConfig.body = JSON.stringify(config.body) + headers: config.headers || new Headers(), + method: config.method || 'POST', + signal, } if (config.body) { + requestConfig.body = JSON.stringify(config.body) requestConfig.headers.set('Content-Type', 'application/json') } - if (abortController) { - requestConfig.signal = abortController.signal - } - const token = loadIdToken() if (token) { - requestConfig.headers.set('x-auth-token', token) + requestConfig.headers.set(AUTH_KEYS.headerToken, token) } return requestConfig diff --git a/src/helpers/callApi/index.tsx b/src/helpers/callApi/index.tsx index fa8ef94f..93cd59a0 100644 --- a/src/helpers/callApi/index.tsx +++ b/src/helpers/callApi/index.tsx @@ -1,18 +1,15 @@ import { TCallApi } from './types' import { parseJSON } from './parseJSON' import { checkStatus } from './checkStatus' -import { removeCookie } from './removeCookie' -import { clearUserAuthInfo } from './clearUserAuthInfo' import { getRequestConfig } from './getRequestConfig' +import { logoutIfUnauthorized } from './logoutIfUnauthorized' -export * from './getResponseData' - -export const callApi = ({ - abortController, +export const callApiBase = ({ + abortSignal, config, url, }: TCallApi) => { - const requestConfig = getRequestConfig(config, abortController) + const requestConfig = getRequestConfig(config, abortSignal) // eslint-disable-next-line no-console console.log( @@ -22,21 +19,18 @@ export const callApi = ({ ) return fetch(url, requestConfig) - .then(checkStatus) - .then(parseJSON) - .catch((error) => { - if (error.message === 'Unauthorized') { - clearUserAuthInfo() - removeCookie('token') - - if (window.location.pathname !== '/login') { - localStorage.setItem('backLocation', window.location.pathname) - window.location.pathname = '/login' - } - } - - // eslint-disable-next-line no-console - console.error(error) - return Promise.reject(error) - }) } + +export const callApi = ({ + abortSignal, + config, + url, +}: TCallApi) => ( + callApiBase({ + abortSignal, + config, + url, + }).then(checkStatus) + .then(parseJSON) + .catch(logoutIfUnauthorized) +) diff --git a/src/helpers/callApi/logoutIfUnauthorized.tsx b/src/helpers/callApi/logoutIfUnauthorized.tsx new file mode 100644 index 00000000..a666d742 --- /dev/null +++ b/src/helpers/callApi/logoutIfUnauthorized.tsx @@ -0,0 +1,22 @@ +import { AUTH_KEYS, PAGES } from 'config' + +import { removeCookie } from './removeCookie' +import { clearUserAuthInfo } from './clearUserAuthInfo' + +export const logoutIfUnauthorized = (error: Error) => { + if (error.message === 'Unauthorized') { + clearUserAuthInfo() + removeCookie(AUTH_KEYS.cookieToken) + + const { pathname } = window.location + + if (pathname !== PAGES.login) { + localStorage.setItem(AUTH_KEYS.backLocation, pathname) + window.location.pathname = PAGES.login + } + } + + // eslint-disable-next-line no-console + console.error(error) + return Promise.reject(error) +} diff --git a/src/helpers/callApi/types.tsx b/src/helpers/callApi/types.tsx index cb2b105a..fe2212b3 100644 --- a/src/helpers/callApi/types.tsx +++ b/src/helpers/callApi/types.tsx @@ -1,14 +1,11 @@ export type TRequestConfig = { - [key: string]: any, body?: any, headers?: Headers, - signal?: any, + method?: string, } -export type TRequestAbortController = AbortController | null - export type TCallApi = { - abortController?: TRequestAbortController, + abortSignal?: AbortSignal, config: TRequestConfig, url: string, }