fix(#2290): add new hook to auth and types

keep-around/1d06f55c2b729e25cd8684a146a86df6860d2876
Andrei Dekterev 4 years ago
parent 04609ac480
commit 1d06f55c2b
  1. 47
      src/features/AuthServiceApp/components/Login/hooks.tsx
  2. 33
      src/features/AuthServiceApp/requests/auth.tsx
  3. 27
      src/features/AuthServiceApp/requests/register.tsx
  4. 17
      src/features/AuthStore/helpers.tsx

@ -7,17 +7,21 @@ import {
import { loginCheck } from 'features/AuthServiceApp/requests/auth' import { loginCheck } from 'features/AuthServiceApp/requests/auth'
import { getApiUrl } from 'features/AuthServiceApp/config/routes' import { getApiUrl } from 'features/AuthServiceApp/config/routes'
import { useLexicsStore } from 'features/LexicsStore'
import { useAuthFields } from 'features/AuthServiceApp/hooks/useAuthFields' import { useAuthFields } from 'features/AuthServiceApp/hooks/useAuthFields'
import { getClientSettings } from 'features/AuthStore/helpers'
import { useLocation } from 'react-router' import { useParamsUrl } from '../../hooks/useParamsUrl'
const url = getApiUrl('/authorize') const url = getApiUrl('/authorize')
export const useLoginForm = () => { export const useLoginForm = () => {
const location = useLocation() const {
const { lang } = useLexicsStore() client_id,
lang,
redirect_uri,
response_mode,
response_type,
scope,
} = useParamsUrl()
const [authError, setAuthError] = useState('') const [authError, setAuthError] = useState('')
const [isFetching, setIsFetching] = useState(false) const [isFetching, setIsFetching] = useState(false)
const [isModalOpen, setIsModalOpen] = useState(false) const [isModalOpen, setIsModalOpen] = useState(false)
@ -47,20 +51,6 @@ export const useLoginForm = () => {
} }
const submitForm = () => formRef.current?.submit() const submitForm = () => formRef.current?.submit()
const redirect_url = new URLSearchParams(location.search).get('redirect_uri')
const clientId = new URLSearchParams(location.search).get('client_id')
const {
client_id,
redirect_uri,
response_mode,
response_type,
scope,
} = getClientSettings()
const currentRedirectUrl = redirect_url ?? redirect_uri
const currentClientId = clientId ?? client_id
const handleError = (error: string) => { const handleError = (error: string) => {
setAuthError(error) setAuthError(error)
setIsFetching(false) setIsFetching(false)
@ -70,13 +60,18 @@ export const useLoginForm = () => {
e.preventDefault() e.preventDefault()
setIsFetching(true) setIsFetching(true)
try { try {
await loginCheck( await loginCheck({
currentClientId,
email, email,
lang,
password, password,
currentRedirectUrl, urlParams: {
) client_id,
lang,
redirect_uri,
response_mode,
response_type,
scope,
},
})
submitForm() submitForm()
} catch (err) { } catch (err) {
handleError(String(err)) handleError(String(err))
@ -89,7 +84,7 @@ export const useLoginForm = () => {
return { return {
authError, authError,
client_id: currentClientId, client_id,
email, email,
formError, formError,
formRef, formRef,
@ -104,7 +99,7 @@ export const useLoginForm = () => {
onPasswordBlur, onPasswordBlur,
onPasswordChange, onPasswordChange,
password, password,
redirect_uri: currentRedirectUrl, redirect_uri,
response_mode, response_mode,
response_type, response_type,
scope, scope,

@ -1,7 +1,7 @@
import { getClientSettings } from 'features/AuthStore/helpers'
import { getApiUrl } from '../config/routes' import { getApiUrl } from '../config/routes'
import type { UrlParams } from './register'
const errorLexics = { const errorLexics = {
1: 'error_invalid_email_or_password', 1: 'error_invalid_email_or_password',
4: 'error_user_not_found', 4: 'error_user_not_found',
@ -20,35 +20,24 @@ type SuccessResponse = {
ok: true, ok: true,
} }
export const loginCheck = async ( type LoginProps = {
currentClientId: string | undefined,
email: string, email: string,
lang: string,
password: string, password: string,
currentRedirectUrl: string | undefined, urlParams: UrlParams,
) => { }
const {
response_mode,
response_type,
scope,
} = getClientSettings()
const paramsUrl = {
client_id: currentClientId,
lang,
redirect_uri: currentRedirectUrl,
response_mode,
response_type,
scope,
}
export const loginCheck = async ({
email,
password,
urlParams,
}: LoginProps) => {
const url = getApiUrl('/authorize-check') const url = getApiUrl('/authorize-check')
const init: RequestInit = { const init: RequestInit = {
body: new URLSearchParams({ body: new URLSearchParams({
email, email,
password, password,
...paramsUrl as {}, ...urlParams,
}), }),
method: 'POST', method: 'POST',
} }

@ -1,3 +1,6 @@
import type { ClientIds } from 'config/clients/types'
import type { Languages } from 'config/languages'
import { getApiUrl } from 'features/AuthServiceApp/config/routes' import { getApiUrl } from 'features/AuthServiceApp/config/routes'
const errorLexics = { const errorLexics = {
@ -21,19 +24,21 @@ type SuccessResponse = {
ok: true, ok: true,
} }
export type UrlParams = {
client_id: ClientIds,
lang?: Languages,
nonce?: string,
redirect_uri: string,
response_mode: string,
response_type: string,
scope?: string,
state?: string,
}
type RegisterProps = { type RegisterProps = {
email: string, email: string,
lang?: string | undefined,
password: string, password: string,
urlParams: { urlParams: UrlParams,
client_id?: string,
none?: string,
redirect_uri?: string,
response_mode?: string,
response_type?: string,
scope?: string,
state?: string,
},
} }
export const registerCheck = async ({ export const registerCheck = async ({
@ -47,7 +52,7 @@ export const registerCheck = async ({
body: new URLSearchParams({ body: new URLSearchParams({
email, email,
password, password,
...urlParams as {}, ...urlParams,
}), }),
method: 'POST', method: 'POST',
} }

@ -3,9 +3,22 @@ import { WebStorageStateStore } from 'oidc-client'
import { client } from 'config/clients' import { client } from 'config/clients'
import { AUTH_SERVICE } from 'config/routes' import { AUTH_SERVICE } from 'config/routes'
import { ClientIds } from 'config/clients/types'
import { ENV } from 'config/env' import { ENV } from 'config/env'
import type { Languages } from 'config/languages'
interface Settings extends UserManagerSettings {
client_id: ClientIds,
lang?: Languages,
nonce?: string,
redirect_uri: string,
response_mode: string,
response_type: string,
scope?: string,
state?: string,
}
const redirectUrl = () => { const redirectUrl = () => {
if (process.env.NODE_ENV === 'development') { if (process.env.NODE_ENV === 'development') {
return `${window.origin}/redirect` return `${window.origin}/redirect`
@ -16,7 +29,7 @@ const redirectUrl = () => {
return `https://${client.name}.tv/redirect` return `https://${client.name}.tv/redirect`
} }
export const getClientSettings = (): UserManagerSettings => ({ export const getClientSettings = (): Settings => ({
authority: AUTH_SERVICE, authority: AUTH_SERVICE,
automaticSilentRenew: true, automaticSilentRenew: true,
client_id: client.auth.clientId, client_id: client.auth.clientId,

Loading…
Cancel
Save