auth
This commit is contained in:
parent
f6174dafea
commit
f27d98607e
@ -20,9 +20,9 @@ function App() {
|
|||||||
<div>
|
<div>
|
||||||
<Router>
|
<Router>
|
||||||
<Routes>
|
<Routes>
|
||||||
<Route path="/" element={<AuctionPage />} />
|
<Route path="/auction" element={<AuctionPage />} />
|
||||||
<Route path="/auth" element={<AuthPage />} />
|
<Route path="/auth" element={<AuthPage />} />
|
||||||
<Route path="*" element={<Navigate to="/" replace />} />
|
<Route path="*" element={<Navigate to="/auth" replace />} />
|
||||||
</Routes>
|
</Routes>
|
||||||
</Router>
|
</Router>
|
||||||
</div>
|
</div>
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import React from "react";
|
import React from "react";
|
||||||
|
import { useNavigate } from "react-router-dom";
|
||||||
|
|
||||||
import { ButtonUi, ButtonUiType } from "../../shared/UI/ButtonUi";
|
import { ButtonUi, ButtonUiType } from "../../shared/UI/ButtonUi";
|
||||||
|
|
||||||
@ -8,11 +9,15 @@ import { useFormik } from 'formik';
|
|||||||
import * as yup from 'yup';
|
import * as yup from 'yup';
|
||||||
|
|
||||||
import { api } from "../../query/query";
|
import { api } from "../../query/query";
|
||||||
|
import { setCookie } from "typescript-cookie";
|
||||||
|
import { AuthResponsePayload } from "../../types";
|
||||||
|
|
||||||
import styles from "./authPage.module.scss"
|
import styles from "./authPage.module.scss"
|
||||||
|
|
||||||
export const AuthPage = () => {
|
export const AuthPage = () => {
|
||||||
|
|
||||||
|
const navigate = useNavigate();
|
||||||
|
|
||||||
const schema = yup.object().shape({
|
const schema = yup.object().shape({
|
||||||
username: yup.string()
|
username: yup.string()
|
||||||
.min(3, 'Минимум 3 символа!')
|
.min(3, 'Минимум 3 символа!')
|
||||||
@ -27,18 +32,28 @@ export const AuthPage = () => {
|
|||||||
handleChange,
|
handleChange,
|
||||||
handleBlur,
|
handleBlur,
|
||||||
errors,
|
errors,
|
||||||
values
|
values,
|
||||||
|
setFieldError
|
||||||
} = useFormik({
|
} = useFormik({
|
||||||
initialValues: {
|
initialValues: {
|
||||||
username: '',
|
username: '',
|
||||||
password: '',
|
password: '',
|
||||||
},
|
},
|
||||||
validationSchema: schema,
|
validationSchema: schema,
|
||||||
onSubmit: async (values) => {
|
onSubmit: async (values): Promise<AuthResponsePayload> => {
|
||||||
await api.post('/authorization', {
|
return await api.post('/authorization', {
|
||||||
username: values.username,
|
username: values.username,
|
||||||
password: values.password
|
password: values.password
|
||||||
}).then((res) => {
|
}).then((res) => {
|
||||||
|
if (res.data[0]) {
|
||||||
|
setFieldError('username', res.data[0])
|
||||||
|
setFieldError('password', res.data[0])
|
||||||
|
} else {
|
||||||
|
setCookie('authToken', res.data!.authToken)
|
||||||
|
setCookie('refreshToken', res.data!.refreshToken)
|
||||||
|
navigate("/auction")
|
||||||
|
}
|
||||||
|
return res.data as AuthResponsePayload
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
10
src/types.ts
10
src/types.ts
@ -6,10 +6,16 @@ export interface AuctionItem {
|
|||||||
status: string
|
status: string
|
||||||
}
|
}
|
||||||
|
|
||||||
type socialName = 'faceBook' | 'instagram' | 'whatsapp'
|
|
||||||
|
|
||||||
export type socialType = {
|
export type socialType = {
|
||||||
name: socialName,
|
name: socialName,
|
||||||
img: string,
|
img: string,
|
||||||
href: string
|
href: string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type AuthResponsePayload = {
|
||||||
|
accessToken: string
|
||||||
|
refreshToken: string
|
||||||
|
} |
|
||||||
|
string[]
|
||||||
|
|
||||||
|
export type socialName = 'faceBook' | 'instagram' | 'whatsapp'
|
||||||
|
Loading…
Reference in New Issue
Block a user