fix(#2227): add post request for auth

keep-around/78728aaea3c73604b730226a5c18add5a7bcecd6
Andrei Dekterev 4 years ago
parent 09b701035f
commit 12dde922e1
  1. 31
      src/features/AuthServiceApp/components/Login/hooks.tsx
  2. 43
      src/features/AuthServiceApp/requests/auth.tsx
  3. 61
      src/features/AuthServiceApp/requests/authСheck.tsx
  4. 14
      src/features/AuthServiceApp/requests/register.tsx

@ -5,12 +5,12 @@ import {
useEffect, useEffect,
} from 'react' } from 'react'
import { addLanguageUrlParam } from 'helpers/languageUrlParam' import { loginCheck } from 'features/AuthServiceApp/requests/authСheck'
import { authorize } 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 { useLexicsStore } from 'features/LexicsStore'
import { useAuthFields } from 'features/AuthServiceApp/hooks/useAuthFields' import { useAuthFields } from 'features/AuthServiceApp/hooks/useAuthFields'
import { useLocation } from 'react-router'
const url = getApiUrl('/authorize') const url = getApiUrl('/authorize')
@ -43,7 +43,10 @@ export const useLoginForm = () => {
setIsModalOpen(true) 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) => { const handleError = (error: string) => {
setAuthError(error) setAuthError(error)
@ -53,9 +56,21 @@ export const useLoginForm = () => {
const handleSubmit = async (e: FormEvent<HTMLFormElement>) => { const handleSubmit = async (e: FormEvent<HTMLFormElement>) => {
e.preventDefault() e.preventDefault()
setIsFetching(true) setIsFetching(true)
loginCheck(email, password) try {
.then(submitForm) await loginCheck(
.catch(handleError) email,
lang,
password,
)
await (authorize(
email,
lang,
password,
redirect_url || '',
))
} catch (err) {
handleError(String(err))
}
} }
useEffect(() => { useEffect(() => {
@ -78,6 +93,6 @@ export const useLoginForm = () => {
onPasswordChange, onPasswordChange,
password, password,
setIsModalOpen, setIsModalOpen,
url: addLanguageUrlParam(lang, url), url,
} }
} }

@ -1,37 +1,52 @@
import { getClientSettings } from 'features/AuthStore/helpers'
import { getApiUrl } from '../config/routes' import { getApiUrl } from '../config/routes'
<<<<<<< HEAD
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',
11: 'error_user_not_confirm', 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 = { const paramsUrl = {
error: { client_id,
code: keyof typeof errorLexics, lang,
message?: string, redirect_uri: redirect_url,
}, response_mode,
ok: false, response_type,
} scope,
}
type SuccessResponse = { const url = getApiUrl('/authorize')
ok: true,
}
export const loginCheck = async (email: string, password: string) => {
const url = getApiUrl('/authorize-check')
const init: RequestInit = { const init: RequestInit = {
body: new URLSearchParams({ body: new URLSearchParams({
email, email,
password, password,
...paramsUrl as {},
}), }),
method: 'POST', method: 'POST',
} }
const response = await fetch(url, init) const response = await fetch(url, init)
const body: SuccessResponse | FailedResponse = await response.json() const body = await response.json()
if (body.ok) return Promise.resolve() if (body.ok) return Promise.resolve()
return Promise.reject(errorLexics[body.error.code]) return Promise.reject()
} }

@ -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])
}

@ -45,19 +45,19 @@ export const registerCheck = async ({
const url = getApiUrl('/registration') const url = getApiUrl('/registration')
const paramsUrl = { const paramsUrl = {
client_id: client_id || 'ott-web', client_id,
lang: lang || 'en', lang,
redirect_uri: redirect_uri || 'http://instat.tv/redirect', redirect_uri,
response_mode: response_mode || 'query', response_mode,
response_type: response_type || 'id_token token', response_type,
scope: scope || 'openid', scope,
} }
const init: RequestInit = { const init: RequestInit = {
body: new URLSearchParams({ body: new URLSearchParams({
email, email,
password, password,
...paramsUrl, ...paramsUrl as {},
}), }),
method: 'POST', method: 'POST',
} }

Loading…
Cancel
Save