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

@ -0,0 +1,24 @@
import { API_URL } from '../../config'
import { fallbackTo, isString } from '../../utils/types'
import { SignUp, SignUpBody, SignUpResponse } from './types'
const composeSignUpURL = () => (
API_URL + '/signup?'
)
const composeSignUpBody = (formData: FormData): SignUpBody => ({
email: fallbackTo(formData.get('email'), isString, ''),
password: fallbackTo(formData.get('password'), isString, ''),
name: fallbackTo(formData.get('name'), isString, ''),
surname: fallbackTo(formData.get('surname'), isString, ''),
})
const processSignUp = (data: SignUpResponse): SignUp => {
if (!data.Success) {
throw new Error(data.Message)
}
return true
}
export { composeSignUpURL, composeSignUpBody, processSignUp }

View File

@ -0,0 +1,30 @@
import { isConst, isObject } from '../../utils/types'
type SignUpBody = {
email: string,
password: string,
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',
})
)
type SignUp = boolean
export type { SignUpBody, SignUpResponse, SignUp }
export { isSignUpResponse }

View File

@ -0,0 +1,23 @@
import { isString } from 'react-bootstrap-typeahead/types/utils'
import { API_URL } from '../../config'
import { fallbackTo } from '../../utils/types'
import { Token, TokenResponse } from './types'
const composeTokenURL = () => (
API_URL + '/token?'
)
const composeSignInBody = (formData: FormData) => {
const resFD = new FormData()
resFD.append('username', fallbackTo(formData.get('email'), isString, ''))
resFD.append('password', fallbackTo(formData.get('password'), isString, ''))
return resFD
}
const processToken = (data: TokenResponse): Token => {
return data.access_token
}
export { composeTokenURL, composeSignInBody, processToken }

View File

@ -0,0 +1,17 @@
import { isObject } from '../../utils/types'
type TokenResponse = {
access_token: string
}
const isTokenResponse = (obj: unknown): obj is TokenResponse => (
isObject(obj, {
'access_token': 'string'
})
)
type Token = string
export type { TokenResponse, Token }
export { isTokenResponse }