From 12dde922e1e496c17d160e2698850741202d8b8e Mon Sep 17 00:00:00 2001 From: Andrei Dekterev Date: Tue, 8 Feb 2022 14:10:12 +0700 Subject: [PATCH] fix(#2227): add post request for auth --- .../AuthServiceApp/components/Login/hooks.tsx | 31 +++++++--- src/features/AuthServiceApp/requests/auth.tsx | 43 ++++++++----- .../AuthServiceApp/requests/authСheck.tsx | 61 +++++++++++++++++++ .../AuthServiceApp/requests/register.tsx | 14 ++--- 4 files changed, 120 insertions(+), 29 deletions(-) create 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 e100db81..49de258f 100644 --- a/src/features/AuthServiceApp/components/Login/hooks.tsx +++ b/src/features/AuthServiceApp/components/Login/hooks.tsx @@ -5,12 +5,12 @@ import { useEffect, } from 'react' -import { addLanguageUrlParam } from 'helpers/languageUrlParam' - -import { loginCheck } from 'features/AuthServiceApp/requests/auth' +import { loginCheck } from 'features/AuthServiceApp/requests/authСheck' +import { authorize } from 'features/AuthServiceApp/requests/auth' import { getApiUrl } from 'features/AuthServiceApp/config/routes' import { useLexicsStore } from 'features/LexicsStore' import { useAuthFields } from 'features/AuthServiceApp/hooks/useAuthFields' +import { useLocation } from 'react-router' const url = getApiUrl('/authorize') @@ -43,7 +43,10 @@ export const useLoginForm = () => { setIsModalOpen(true) } - const submitForm = () => formRef.current?.submit() + // const submitForm = () => formRef.current?.submit() + const location = useLocation() + + const redirect_url = new URLSearchParams(location.search).get('redirect_uri') const handleError = (error: string) => { setAuthError(error) @@ -53,9 +56,21 @@ export const useLoginForm = () => { const handleSubmit = async (e: FormEvent) => { e.preventDefault() setIsFetching(true) - loginCheck(email, password) - .then(submitForm) - .catch(handleError) + try { + await loginCheck( + email, + lang, + password, + ) + await (authorize( + email, + lang, + password, + redirect_url || '', + )) + } catch (err) { + handleError(String(err)) + } } useEffect(() => { @@ -78,6 +93,6 @@ export const useLoginForm = () => { onPasswordChange, password, setIsModalOpen, - url: addLanguageUrlParam(lang, url), + url, } } diff --git a/src/features/AuthServiceApp/requests/auth.tsx b/src/features/AuthServiceApp/requests/auth.tsx index bd1126da..efb79631 100644 --- a/src/features/AuthServiceApp/requests/auth.tsx +++ b/src/features/AuthServiceApp/requests/auth.tsx @@ -1,37 +1,52 @@ +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 ( + email: string, + lang: string, + password: string, + redirect_url: string, +) => { + const { + client_id, + response_mode, + response_type, + scope, + } = getClientSettings() +>>>>>>> 91891dc (fix(#2227): add post request for auth) -type FailedResponse = { - error: { - code: keyof typeof errorLexics, - message?: string, - }, - ok: false, -} + const paramsUrl = { + client_id, + lang, + redirect_uri: redirect_url, + response_mode, + response_type, + scope, + } -type SuccessResponse = { - ok: true, -} + const url = getApiUrl('/authorize') -export const loginCheck = async (email: string, password: string) => { - 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() + const body = await response.json() if (body.ok) return Promise.resolve() - return Promise.reject(errorLexics[body.error.code]) + return Promise.reject() } diff --git a/src/features/AuthServiceApp/requests/authСheck.tsx b/src/features/AuthServiceApp/requests/authСheck.tsx new file mode 100644 index 00000000..a7cc24a8 --- /dev/null +++ b/src/features/AuthServiceApp/requests/authСheck.tsx @@ -0,0 +1,61 @@ +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, +) => { + const { + client_id, + redirect_uri, + 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]) +} diff --git a/src/features/AuthServiceApp/requests/register.tsx b/src/features/AuthServiceApp/requests/register.tsx index 12a2935b..c8c936d0 100644 --- a/src/features/AuthServiceApp/requests/register.tsx +++ b/src/features/AuthServiceApp/requests/register.tsx @@ -45,19 +45,19 @@ export const registerCheck = async ({ const url = getApiUrl('/registration') const paramsUrl = { - client_id: client_id || 'ott-web', - lang: lang || 'en', - redirect_uri: redirect_uri || 'http://instat.tv/redirect', - response_mode: response_mode || 'query', - response_type: response_type || 'id_token token', - scope: scope || 'openid', + client_id, + lang, + redirect_uri, + response_mode, + response_type, + scope, } const init: RequestInit = { body: new URLSearchParams({ email, password, - ...paramsUrl, + ...paramsUrl as {}, }), method: 'POST', }