From 9f766476c6b16157fd3c4ee7c060262c27445d87 Mon Sep 17 00:00:00 2001 From: Mirlan Date: Mon, 13 Jul 2020 15:15:54 +0600 Subject: [PATCH] Ott 207 sending jwt (#33) --- src/config/authKeys.tsx | 8 +---- src/helpers/callApi/clearUserAuthInfo.tsx | 6 ---- src/helpers/callApi/getRequestConfig.tsx | 8 ++--- src/helpers/callApi/index.tsx | 6 ++-- src/helpers/callApi/logoutIfUnauthorized.tsx | 17 +++------ src/helpers/callApi/removeCookie.tsx | 11 ------ src/helpers/callApi/types.tsx | 6 ++-- src/requests/login.tsx | 37 +++++++++++++------- 8 files changed, 39 insertions(+), 60 deletions(-) delete mode 100644 src/helpers/callApi/clearUserAuthInfo.tsx delete mode 100644 src/helpers/callApi/removeCookie.tsx diff --git a/src/config/authKeys.tsx b/src/config/authKeys.tsx index a6199593..a3c672fe 100644 --- a/src/config/authKeys.tsx +++ b/src/config/authKeys.tsx @@ -1,7 +1 @@ -export const AUTH_KEYS = { - authUser: 'AuthUser', - backLocation: 'backLocation', - cookieToken: 'token', - headerToken: 'x-auth-token', - idToken: 'id_token', -} +export const TOKEN_KEY = 'auth_token' diff --git a/src/helpers/callApi/clearUserAuthInfo.tsx b/src/helpers/callApi/clearUserAuthInfo.tsx deleted file mode 100644 index 1af1eeed..00000000 --- a/src/helpers/callApi/clearUserAuthInfo.tsx +++ /dev/null @@ -1,6 +0,0 @@ -import { AUTH_KEYS } from 'config' - -export const clearUserAuthInfo = () => { - 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 14ebafab..d182fa66 100644 --- a/src/helpers/callApi/getRequestConfig.tsx +++ b/src/helpers/callApi/getRequestConfig.tsx @@ -1,10 +1,10 @@ -import { AUTH_KEYS } from 'config' +import { TOKEN_KEY } from 'config' import { readToken } from 'helpers' -import { TRequestConfig } from './types' +import type { RequestConfig } from './types' export const getRequestConfig = ( - config: TRequestConfig, + config: RequestConfig, signal?: AbortSignal, ) => { const requestConfig = { @@ -22,7 +22,7 @@ export const getRequestConfig = ( const token = readToken() if (token) { - requestConfig.headers.set(AUTH_KEYS.headerToken, token) + requestConfig.headers.set(TOKEN_KEY, token) } return requestConfig diff --git a/src/helpers/callApi/index.tsx b/src/helpers/callApi/index.tsx index 93cd59a0..76b765f7 100644 --- a/src/helpers/callApi/index.tsx +++ b/src/helpers/callApi/index.tsx @@ -1,4 +1,4 @@ -import { TCallApi } from './types' +import type { CallApiArgs } from './types' import { parseJSON } from './parseJSON' import { checkStatus } from './checkStatus' import { getRequestConfig } from './getRequestConfig' @@ -8,7 +8,7 @@ export const callApiBase = ({ abortSignal, config, url, -}: TCallApi) => { +}: CallApiArgs) => { const requestConfig = getRequestConfig(config, abortSignal) // eslint-disable-next-line no-console @@ -25,7 +25,7 @@ export const callApi = ({ abortSignal, config, url, -}: TCallApi) => ( +}: CallApiArgs) => ( callApiBase({ abortSignal, config, diff --git a/src/helpers/callApi/logoutIfUnauthorized.tsx b/src/helpers/callApi/logoutIfUnauthorized.tsx index a666d742..b98f9e92 100644 --- a/src/helpers/callApi/logoutIfUnauthorized.tsx +++ b/src/helpers/callApi/logoutIfUnauthorized.tsx @@ -1,19 +1,10 @@ -import { AUTH_KEYS, PAGES } from 'config' - -import { removeCookie } from './removeCookie' -import { clearUserAuthInfo } from './clearUserAuthInfo' +import { PAGES } from 'config' +import { removeToken } from 'helpers' 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 - } + removeToken() + window.location.pathname = PAGES.login } // eslint-disable-next-line no-console diff --git a/src/helpers/callApi/removeCookie.tsx b/src/helpers/callApi/removeCookie.tsx deleted file mode 100644 index 4118169c..00000000 --- a/src/helpers/callApi/removeCookie.tsx +++ /dev/null @@ -1,11 +0,0 @@ -export const removeCookie = ( - name: string, - domain: string = '.instatscout.com', -) => { - document.cookie = ` - ${name}=; - expires='Thu, 01 Jan 1970 00:00:00 UTC'; - path=/; - domain=${domain} - ` -} diff --git a/src/helpers/callApi/types.tsx b/src/helpers/callApi/types.tsx index fe2212b3..394be836 100644 --- a/src/helpers/callApi/types.tsx +++ b/src/helpers/callApi/types.tsx @@ -1,11 +1,11 @@ -export type TRequestConfig = { +export type RequestConfig = { body?: any, headers?: Headers, method?: string, } -export type TCallApi = { +export type CallApiArgs = { abortSignal?: AbortSignal, - config: TRequestConfig, + config: RequestConfig, url: string, } diff --git a/src/requests/login.tsx b/src/requests/login.tsx index dd83c27f..f0d95c46 100644 --- a/src/requests/login.tsx +++ b/src/requests/login.tsx @@ -1,5 +1,11 @@ -import { DATA_URL, PROCEDURES } from 'config' -import { callApi, writeToken } from 'helpers' +import { + DATA_URL, + PROCEDURES, + TOKEN_KEY, +} from 'config' +import { callApiBase } from 'helpers/callApi' +import { checkStatus } from 'helpers/callApi/checkStatus' +import { parseJSON } from 'helpers/callApi/parseJSON' const proc = PROCEDURES.auth_user @@ -15,7 +21,6 @@ type StatusCodes = typeof statusCodes[keyof typeof statusCodes] type Response = { _p_status: StatusCodes, - _p_token: string, } type Args = { @@ -37,15 +42,21 @@ export const login = async ({ }, } - const response: Response = await callApi({ - config, - url: DATA_URL, - }) - - if (response._p_status === statusCodes.SUCCESS) { - writeToken(response._p_token) - return Promise.resolve(response._p_token) + try { + const response = await callApiBase({ + config, + url: DATA_URL, + }) + checkStatus(response) + + const token = response.headers.get(TOKEN_KEY) + const { _p_status }: Response = await parseJSON(response) + + if (token && _p_status === statusCodes.SUCCESS) { + return Promise.resolve(token) + } + return Promise.reject(_p_status) + } catch (error) { + return Promise.reject() } - - return Promise.reject(response._p_status) }