From e57e645fc65cd602ae9ae8ef6c00c193ea66ca98 Mon Sep 17 00:00:00 2001 From: Andrei Dekterev Date: Thu, 10 Feb 2022 21:11:01 +0700 Subject: [PATCH] fix(#2227): fix post request to auth --- .../AuthServiceApp/components/Login/hooks.tsx | 32 +++++++++++++------ .../AuthServiceApp/components/Login/index.tsx | 17 ++++++++++ .../AuthServiceApp/requests/authСheck.tsx | 4 +-- src/features/AuthStore/helpers.tsx | 4 ++- 4 files changed, 45 insertions(+), 12 deletions(-) diff --git a/src/features/AuthServiceApp/components/Login/hooks.tsx b/src/features/AuthServiceApp/components/Login/hooks.tsx index 61c96d57..419869b6 100644 --- a/src/features/AuthServiceApp/components/Login/hooks.tsx +++ b/src/features/AuthServiceApp/components/Login/hooks.tsx @@ -6,10 +6,11 @@ import { } from 'react' 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 { getClientSettings } from 'features/AuthStore/helpers' + import { useLocation } from 'react-router' const url = getApiUrl('/authorize') @@ -22,6 +23,7 @@ export const useLoginForm = () => { const [isModalOpen, setIsModalOpen] = useState(false) const formRef = useRef(null) + const { email, error: formError, @@ -43,8 +45,19 @@ export const useLoginForm = () => { const handleModalOpen = () => { setIsModalOpen(true) } + const submitForm = () => formRef.current?.submit() + + const redirect_url = new URLSearchParams(location.search).get('redirect_uri') + + const { + client_id, + redirect_uri, + response_mode, + response_type, + scope, + } = getClientSettings() - const redirect_url = new URLSearchParams(location.search).get('redirect_uri') || '' + const currentRedirectUrl = redirect_url ?? redirect_uri const handleError = (error: string) => { setAuthError(error) @@ -59,14 +72,9 @@ export const useLoginForm = () => { email, lang, password, - redirect_url, + currentRedirectUrl, ) - await (authorize( - email, - lang, - password, - redirect_url, - )) + submitForm() } catch (err) { handleError(String(err)) } @@ -78,6 +86,7 @@ export const useLoginForm = () => { return { authError, + client_id, email, formError, formRef, @@ -86,11 +95,16 @@ export const useLoginForm = () => { isFetching, isModalOpen, isSubmitDisabled, + lang, onEmailBlur, onEmailChange, onPasswordBlur, onPasswordChange, password, + redirect_uri: currentRedirectUrl, + response_mode, + response_type, + scope, setIsModalOpen, url, } diff --git a/src/features/AuthServiceApp/components/Login/index.tsx b/src/features/AuthServiceApp/components/Login/index.tsx index 65977ca9..1f5215e8 100644 --- a/src/features/AuthServiceApp/components/Login/index.tsx +++ b/src/features/AuthServiceApp/components/Login/index.tsx @@ -25,25 +25,36 @@ import { RegisterButton } from './styled' const Login = () => { const { authError, + client_id, email, formError, + formRef, handleModalOpen, handleSubmit, isFetching, isModalOpen, isSubmitDisabled, + lang, onEmailBlur, onEmailChange, onPasswordBlur, onPasswordChange, password, + redirect_uri, + response_mode, + response_type, + scope, setIsModalOpen, + url, } = useLoginForm() return (
@@ -68,6 +79,12 @@ const Login = () => { onChange={onPasswordChange} onBlur={onPasswordBlur} /> + + + + + + diff --git a/src/features/AuthServiceApp/requests/authСheck.tsx b/src/features/AuthServiceApp/requests/authСheck.tsx index 218d167c..d5288ed4 100644 --- a/src/features/AuthServiceApp/requests/authСheck.tsx +++ b/src/features/AuthServiceApp/requests/authСheck.tsx @@ -23,7 +23,7 @@ export const loginCheck = async ( email: string, lang: string, password: string, - redirect_url: string, + redirect_uri: string | undefined, ) => { const { client_id, @@ -35,7 +35,7 @@ export const loginCheck = async ( const paramsUrl = { client_id, lang, - redirect_uri: redirect_url, + redirect_uri, response_mode, response_type, scope, diff --git a/src/features/AuthStore/helpers.tsx b/src/features/AuthStore/helpers.tsx index 4d1c6446..e11b0956 100644 --- a/src/features/AuthStore/helpers.tsx +++ b/src/features/AuthStore/helpers.tsx @@ -4,6 +4,8 @@ import { WebStorageStateStore } from 'oidc-client' import { client } from 'config/clients' import { AUTH_SERVICE } from 'config/routes' +import { ENV } from 'config/env' + export const getClientSettings = (): UserManagerSettings => ({ authority: AUTH_SERVICE, automaticSilentRenew: true, @@ -11,7 +13,7 @@ export const getClientSettings = (): UserManagerSettings => ({ filterProtocolClaims: false, loadUserInfo: false, metadataUrl: `${AUTH_SERVICE}/.well-known/openid-configuration${client.auth.metaDataUrlParams || ''}`, - redirect_uri: `${window.origin}/redirect`, + redirect_uri: ENV === 'staging' ? `https://${ENV}.${client.name}.tv/redirect` : `https://${client.name}.tv/redirect`, response_mode: 'query', response_type: 'id_token token', scope: 'openid',