diff --git a/src/features/AuthServiceApp/components/ChangePassword/hooks.tsx b/src/features/AuthServiceApp/components/ChangePassword/hooks.tsx index cfcca3f3..73e6de32 100644 --- a/src/features/AuthServiceApp/components/ChangePassword/hooks.tsx +++ b/src/features/AuthServiceApp/components/ChangePassword/hooks.tsx @@ -4,10 +4,17 @@ import { useState, } from 'react' +import { useLocation, useHistory } from 'react-router' + +import { ClientIds } from 'config/clients/types' + import { useAuthFields } from 'features/AuthServiceApp/hooks/useAuthFields' import { changePassword } from 'features/AuthServiceApp/requests/changePassword' +import { getClientSettings } from 'features/AuthStore/helpers' export const useChangePasswordForm = () => { + const location = useLocation() + const history = useHistory() const [error, setError] = useState('') const [isFetching, setIsFetching] = useState(false) const [password, setPassword] = useState('') @@ -18,16 +25,18 @@ export const useChangePasswordForm = () => { checkPassword, } = useAuthFields('login') + const { client_id } = getClientSettings() + + const currentClientId = new URLSearchParams(location.search).get('client_id') ?? client_id + const handleSubmit = async (event: SyntheticEvent) => { event?.preventDefault() setIsFetching(true) try { - const redirectUrl = await changePassword(password) + await changePassword(currentClientId as ClientIds, password) setError('') setIsFetching(false) - if (redirectUrl) { - await window.location.assign(redirectUrl) - } + history.push(`/autorize?client_id=${currentClientId}`) } catch (err) { setError(String(err)) setIsFetching(false) diff --git a/src/features/AuthServiceApp/components/RecoveryPopup/hooks.tsx b/src/features/AuthServiceApp/components/RecoveryPopup/hooks.tsx index ec0135d4..2cb85698 100644 --- a/src/features/AuthServiceApp/components/RecoveryPopup/hooks.tsx +++ b/src/features/AuthServiceApp/components/RecoveryPopup/hooks.tsx @@ -47,8 +47,10 @@ export const useRecovery = (setIsModalOpen: (value: boolean) => void) => { const handleSubmit = () => { setIsFetching(true) loginCheckChangePass(email, lang) - .then(() => setIsFetching(false)) - .then(() => setIsSendMessage(true)) + .then(() => { + setIsFetching(false) + setIsSendMessage(true) + }) .catch(handleError) } @@ -58,7 +60,7 @@ export const useRecovery = (setIsModalOpen: (value: boolean) => void) => { } else { setIsSuccess(false) } - }, [isSendMessage, isFetching]) + }, [isFetching, isSendMessage]) return { closePopup, diff --git a/src/features/AuthServiceApp/requests/changePassword.tsx b/src/features/AuthServiceApp/requests/changePassword.tsx index 03ca8564..4a07268a 100644 --- a/src/features/AuthServiceApp/requests/changePassword.tsx +++ b/src/features/AuthServiceApp/requests/changePassword.tsx @@ -1,3 +1,4 @@ +import { ClientIds } from 'config/clients/types' import { getApiUrl } from '../config/routes' const errorLexics = { @@ -14,16 +15,14 @@ type FailedResponse = { } type SuccessResponse = { - data: { - url: string, - }, ok: true, } -export const changePassword = async (password: string) => { +export const changePassword = async (currentClientId: ClientIds, password: string) => { const url = getApiUrl('/change_password') const init: RequestInit = { body: new URLSearchParams({ + client_id: currentClientId, password, }), method: 'PUT', @@ -32,7 +31,7 @@ export const changePassword = async (password: string) => { const body: SuccessResponse | FailedResponse = await response.json() - if (body.ok) return body.data.url + if (body.ok) return Promise.resolve() return Promise.reject(errorLexics[body.error.code]) }