Connected signing up and signing in to back
This commit is contained in:
24
front/src/api/signup/index.ts
Normal file
24
front/src/api/signup/index.ts
Normal 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 }
|
30
front/src/api/signup/types.ts
Normal file
30
front/src/api/signup/types.ts
Normal 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 }
|
23
front/src/api/token/index.ts
Normal file
23
front/src/api/token/index.ts
Normal 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 }
|
17
front/src/api/token/types.ts
Normal file
17
front/src/api/token/types.ts
Normal 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 }
|
Reference in New Issue
Block a user