From 78728aaea3c73604b730226a5c18add5a7bcecd6 Mon Sep 17 00:00:00 2001 From: Andrei Dekterev Date: Mon, 14 Feb 2022 16:40:40 +0700 Subject: [PATCH] fix(#2227): fix auth file --- .../AuthServiceApp/components/Login/hooks.tsx | 2 +- src/features/AuthServiceApp/requests/auth.tsx | 28 ++++++--- .../AuthServiceApp/requests/authСheck.tsx | 61 ------------------- 3 files changed, 20 insertions(+), 71 deletions(-) delete mode 100644 src/features/AuthServiceApp/requests/authСheck.tsx diff --git a/src/features/AuthServiceApp/components/Login/hooks.tsx b/src/features/AuthServiceApp/components/Login/hooks.tsx index 419869b6..184625a9 100644 --- a/src/features/AuthServiceApp/components/Login/hooks.tsx +++ b/src/features/AuthServiceApp/components/Login/hooks.tsx @@ -5,7 +5,7 @@ import { useEffect, } from 'react' -import { loginCheck } from 'features/AuthServiceApp/requests/authСheck' +import { loginCheck } from 'features/AuthServiceApp/requests/auth' import { getApiUrl } from 'features/AuthServiceApp/config/routes' import { useLexicsStore } from 'features/LexicsStore' import { useAuthFields } from 'features/AuthServiceApp/hooks/useAuthFields' diff --git a/src/features/AuthServiceApp/requests/auth.tsx b/src/features/AuthServiceApp/requests/auth.tsx index efb79631..1f2be78b 100644 --- a/src/features/AuthServiceApp/requests/auth.tsx +++ b/src/features/AuthServiceApp/requests/auth.tsx @@ -2,18 +2,29 @@ import { getClientSettings } from 'features/AuthStore/helpers' import { getApiUrl } from '../config/routes' -<<<<<<< HEAD const errorLexics = { 1: 'error_invalid_email_or_password', 4: 'error_user_not_found', 11: 'error_user_not_confirm', } -======= -export const authorize = async ( + +type FailedResponse = { + error: { + code: keyof typeof errorLexics, + message?: string, + }, + ok: false, +} + +type SuccessResponse = { + ok: true, +} + +export const loginCheck = async ( email: string, lang: string, password: string, - redirect_url: string, + redirect_uri: string | undefined, ) => { const { client_id, @@ -21,18 +32,17 @@ export const authorize = async ( response_type, scope, } = getClientSettings() ->>>>>>> 91891dc (fix(#2227): add post request for auth) const paramsUrl = { client_id, lang, - redirect_uri: redirect_url, + redirect_uri, response_mode, response_type, scope, } - const url = getApiUrl('/authorize') + const url = getApiUrl('/authorize-check') const init: RequestInit = { body: new URLSearchParams({ @@ -44,9 +54,9 @@ export const authorize = async ( } const response = await fetch(url, init) - const body = await response.json() + const body: SuccessResponse | FailedResponse = await response.json() if (body.ok) return Promise.resolve() - return Promise.reject() + return Promise.reject(errorLexics[body.error.code]) } diff --git a/src/features/AuthServiceApp/requests/authСheck.tsx b/src/features/AuthServiceApp/requests/authСheck.tsx deleted file mode 100644 index d5288ed4..00000000 --- a/src/features/AuthServiceApp/requests/authСheck.tsx +++ /dev/null @@ -1,61 +0,0 @@ -import { getClientSettings } from 'features/AuthStore/helpers' - -import { getApiUrl } from '../config/routes' - -const errorLexics = { - 1: 'error_invalid_email_or_password', - 4: 'error_user_not_found', -} - -type FailedResponse = { - error: { - code: keyof typeof errorLexics, - message?: string, - }, - ok: false, -} - -type SuccessResponse = { - ok: true, -} - -export const loginCheck = async ( - email: string, - lang: string, - password: string, - redirect_uri: string | undefined, -) => { - const { - client_id, - response_mode, - response_type, - scope, - } = getClientSettings() - - const paramsUrl = { - client_id, - lang, - redirect_uri, - response_mode, - response_type, - scope, - } - - const url = getApiUrl('/authorize-check') - - const init: RequestInit = { - body: new URLSearchParams({ - email, - password, - ...paramsUrl as {}, - }), - method: 'POST', - } - const response = await fetch(url, init) - - const body: SuccessResponse | FailedResponse = await response.json() - - if (body.ok) return Promise.resolve() - - return Promise.reject(errorLexics[body.error.code]) -}