From 04609ac480a48a0ac646eb509cc52422bc229295 Mon Sep 17 00:00:00 2001 From: Andrei Dekterev Date: Mon, 28 Feb 2022 20:45:01 +0700 Subject: [PATCH] fix(2290): fix send post params from url in registration --- .../components/Registration/hooks.tsx | 6 ++-- .../AuthServiceApp/hooks/useParamsUrl.tsx | 31 +++++++++++++++++ .../AuthServiceApp/requests/register.tsx | 34 +++++++------------ 3 files changed, 46 insertions(+), 25 deletions(-) create mode 100644 src/features/AuthServiceApp/hooks/useParamsUrl.tsx diff --git a/src/features/AuthServiceApp/components/Registration/hooks.tsx b/src/features/AuthServiceApp/components/Registration/hooks.tsx index c36e199d..ea21bb12 100644 --- a/src/features/AuthServiceApp/components/Registration/hooks.tsx +++ b/src/features/AuthServiceApp/components/Registration/hooks.tsx @@ -1,11 +1,12 @@ import { SyntheticEvent, useState } from 'react' import { useAuthFields } from 'features/AuthServiceApp/hooks/useAuthFields' -import { useLexicsStore } from 'features/LexicsStore' import { registerCheck } from '../../requests/register' +import { useParamsUrl } from '../../hooks/useParamsUrl' export const useRegistrationForm = () => { + const urlParams = useParamsUrl() const [authError, setAuthError] = useState('') const [termsAccepted, setTermsAccepted] = useState(false) const [isFetching, setIsFetching] = useState(false) @@ -27,7 +28,6 @@ export const useRegistrationForm = () => { || Boolean(formError) || isFetching ) - const { lang } = useLexicsStore() const handleSubmit = async (event: SyntheticEvent) => { event?.preventDefault() @@ -35,8 +35,8 @@ export const useRegistrationForm = () => { try { await registerCheck({ email, - lang, password, + urlParams, }) setAuthError('') setIsFetching(false) diff --git a/src/features/AuthServiceApp/hooks/useParamsUrl.tsx b/src/features/AuthServiceApp/hooks/useParamsUrl.tsx new file mode 100644 index 00000000..165ea767 --- /dev/null +++ b/src/features/AuthServiceApp/hooks/useParamsUrl.tsx @@ -0,0 +1,31 @@ +import { useLocation } from 'react-router' + +import { getClientSettings } from 'features/AuthStore/helpers' +import { useLexicsStore } from 'features/LexicsStore' + +export const useParamsUrl = () => { + const location = useLocation() + const { lang } = useLexicsStore() + + const { + client_id, + redirect_uri, + response_mode, + response_type, + scope, + } = getClientSettings() + + const urlSearchParams = new URLSearchParams(location.search) + + const params = Object.fromEntries(urlSearchParams.entries()) + + return { + client_id, + redirect_uri, + response_mode, + response_type, + scope, + ...params, + lang, + } +} diff --git a/src/features/AuthServiceApp/requests/register.tsx b/src/features/AuthServiceApp/requests/register.tsx index c8c936d0..f0b45c13 100644 --- a/src/features/AuthServiceApp/requests/register.tsx +++ b/src/features/AuthServiceApp/requests/register.tsx @@ -1,7 +1,5 @@ import { getApiUrl } from 'features/AuthServiceApp/config/routes' -import { getClientSettings } from 'features/AuthStore/helpers' - const errorLexics = { 1: 'error_invalid_email_or_password', 2: 'error_missing_required_argument', @@ -25,39 +23,31 @@ type SuccessResponse = { type RegisterProps = { email: string, - lang?: string, + lang?: string | undefined, password: string, + urlParams: { + client_id?: string, + none?: string, + redirect_uri?: string, + response_mode?: string, + response_type?: string, + scope?: string, + state?: string, + }, } export const registerCheck = async ({ email, - lang, password, + urlParams, } : RegisterProps) => { - const { - client_id, - redirect_uri, - response_mode, - response_type, - scope, - } = getClientSettings() - const url = getApiUrl('/registration') - const paramsUrl = { - client_id, - lang, - redirect_uri, - response_mode, - response_type, - scope, - } - const init: RequestInit = { body: new URLSearchParams({ email, password, - ...paramsUrl as {}, + ...urlParams as {}, }), method: 'POST', }