Connected signing up and signing in to back

This commit is contained in:
2023-07-29 10:41:54 +03:00
parent 0e5aeae491
commit 85472233a3
14 changed files with 228 additions and 168 deletions

View File

@@ -1,8 +1,9 @@
export { default as useAnnouncements } from './useAnnouncements'
export { default as useBook } from './useBook'
export { default as useAuth } from './useAuth'
export { default as useTrashboxes } from './useTrashboxes'
export { default as useAddAnnouncement } from './useAddAnnouncement'
export { default as useOsmAddresses } from './useOsmAddress'
export { default as useUser } from './useUser'
export { default as useRemoveAnnouncement } from './useRemoveAnnouncement'
export { default as useSignIn } from './useSignIn'
export { default as useSignUp } from './useSignUp'

View File

@@ -1,117 +0,0 @@
import { useState } from 'react'
import { API_URL } from '../../config'
import { isConst, isObject } from '../../utils/types'
import { handleHTTPErrors } from '../../utils'
interface AuthData {
email: string,
password: string,
}
// interface LoginData extends AuthData { }
// interface SignUpData extends AuthData {
// name: string,
// surname: string
// }
type SignUpResponse = {
Success: true
} | {
Success: false,
Message: string
}
const isSignUpResponse = (obj: unknown): obj is SignUpResponse => (
isObject(obj, {
'Success': isConst(true)
}) ||
isObject(obj, {
'Success': isConst(false),
'Message': 'string'
})
)
interface LogInResponse {
access_token: string,
token_type: 'bearer'
}
const isLogInResponse = (obj: unknown): obj is LogInResponse => (
isObject(obj, {
'access_token': 'string',
'token_type': isConst('bearer')
})
)
function useAuth() {
const [loading, setLoading] = useState(false)
const [error, setError] = useState('')
async function doAuth(data: AuthData, newAccount: boolean) {
setLoading(true)
if (newAccount) {
try {
const res = await fetch(API_URL + '/signup', {
method: 'POST',
body: JSON.stringify(data),
headers: {
'Content-Type': 'application/json'
}
})
handleHTTPErrors(res)
const signupData: unknown = await res.json()
if (!isSignUpResponse(signupData)) {
throw new Error('Malformed server response')
}
if (signupData.Success === false) {
throw new Error(signupData.Message)
}
} catch (err) {
setError(err instanceof Error ? err.message : err as string)
setLoading(false)
return null
}
}
try {
const res = await fetch(API_URL + '/auth/token' + new URLSearchParams({
username: data.email,
password: data.password
}).toString(), {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
})
const logInData: unknown = await res.json()
if (!isLogInResponse(logInData)) {
throw new Error('Malformed server response')
}
const token = logInData.access_token
setError('')
setLoading(false)
return token
} catch (err) {
setError(err instanceof Error ? err.message : err as string)
setLoading(false)
return null
}
}
return { doAuth, loading, error }
}
export default useAuth

View File

@@ -0,0 +1,31 @@
import { useSendWithButton } from '..'
import { composeTokenURL, processToken } from '../../api/token'
import { isTokenResponse } from '../../api/token/types'
import { setToken } from '../../utils/auth'
function useSignIn() {
const { doSend, button } = useSendWithButton(
'Войти',
'Войдено',
false,
composeTokenURL(),
'POST',
false,
isTokenResponse,
processToken,
)
async function handleSignIn(formData: FormData) {
const token = await doSend({}, {
body: formData
})
if (token !== undefined) {
setToken(token)
}
}
return { handleSignIn, signInButton: button }
}
export default useSignIn

View File

@@ -0,0 +1,31 @@
import { useSendWithButton } from '..'
import { composeSignUpURL, processSignUp } from '../../api/signup'
import { SignUpBody, isSignUpResponse } from '../../api/signup/types'
function useSignUp() {
const { doSend, button } = useSendWithButton(
'Зарегистрироваться',
'Зарегистрирован',
false,
composeSignUpURL(),
'POST',
false,
isSignUpResponse,
processSignUp,
)
async function handleSignUp(data: SignUpBody) {
const res = await doSend({}, {
body: JSON.stringify(data),
headers: {
'Content-Type': 'application/json'
}
})
return res ?? false
}
return { handleSignUp, signUpButton: button }
}
export default useSignUp