fix(#2227): fix auth file

keep-around/78728aaea3c73604b730226a5c18add5a7bcecd6
Andrei Dekterev 4 years ago
parent e57e645fc6
commit 78728aaea3
  1. 2
      src/features/AuthServiceApp/components/Login/hooks.tsx
  2. 28
      src/features/AuthServiceApp/requests/auth.tsx
  3. 61
      src/features/AuthServiceApp/requests/authСheck.tsx

@ -5,7 +5,7 @@ import {
useEffect, useEffect,
} from 'react' } from 'react'
import { loginCheck } from 'features/AuthServiceApp/requests/authСheck' 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'

@ -2,18 +2,29 @@ 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 ( type FailedResponse = {
error: {
code: keyof typeof errorLexics,
message?: string,
},
ok: false,
}
type SuccessResponse = {
ok: true,
}
export const loginCheck = async (
email: string, email: string,
lang: string, lang: string,
password: string, password: string,
redirect_url: string, redirect_uri: string | undefined,
) => { ) => {
const { const {
client_id, client_id,
@ -21,18 +32,17 @@ export const authorize = async (
response_type, response_type,
scope, scope,
} = getClientSettings() } = getClientSettings()
>>>>>>> 91891dc (fix(#2227): add post request for auth)
const paramsUrl = { const paramsUrl = {
client_id, client_id,
lang, lang,
redirect_uri: redirect_url, redirect_uri,
response_mode, response_mode,
response_type, response_type,
scope, scope,
} }
const url = getApiUrl('/authorize') const url = getApiUrl('/authorize-check')
const init: RequestInit = { const init: RequestInit = {
body: new URLSearchParams({ body: new URLSearchParams({
@ -44,9 +54,9 @@ export const authorize = async (
} }
const response = await fetch(url, init) const response = await fetch(url, init)
const body = await response.json() const body: SuccessResponse | FailedResponse = await response.json()
if (body.ok) return Promise.resolve() if (body.ok) return Promise.resolve()
return Promise.reject() return Promise.reject(errorLexics[body.error.code])
} }

@ -1,61 +0,0 @@
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,
redirect_uri: string | undefined,
) => {
const {
client_id,
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])
}
Loading…
Cancel
Save