From f27d98607e25473a613f9af02f13bcdb06f84e04 Mon Sep 17 00:00:00 2001 From: Mikola Date: Mon, 18 Dec 2023 22:21:21 +0300 Subject: [PATCH] auth --- src/app/App.tsx | 4 ++-- src/pages/AuthPage/AuthPage.tsx | 21 ++++++++++++++++++--- src/types.ts | 10 ++++++++-- 3 files changed, 28 insertions(+), 7 deletions(-) diff --git a/src/app/App.tsx b/src/app/App.tsx index 488c0e7..9a70839 100644 --- a/src/app/App.tsx +++ b/src/app/App.tsx @@ -20,9 +20,9 @@ function App() {
- } /> + } /> } /> - } /> + } />
diff --git a/src/pages/AuthPage/AuthPage.tsx b/src/pages/AuthPage/AuthPage.tsx index 46ad33d..d73f538 100644 --- a/src/pages/AuthPage/AuthPage.tsx +++ b/src/pages/AuthPage/AuthPage.tsx @@ -1,4 +1,5 @@ import React from "react"; +import { useNavigate } from "react-router-dom"; import { ButtonUi, ButtonUiType } from "../../shared/UI/ButtonUi"; @@ -8,11 +9,15 @@ import { useFormik } from 'formik'; import * as yup from 'yup'; import { api } from "../../query/query"; +import { setCookie } from "typescript-cookie"; +import { AuthResponsePayload } from "../../types"; import styles from "./authPage.module.scss" export const AuthPage = () => { + const navigate = useNavigate(); + const schema = yup.object().shape({ username: yup.string() .min(3, 'Минимум 3 символа!') @@ -27,18 +32,28 @@ export const AuthPage = () => { handleChange, handleBlur, errors, - values + values, + setFieldError } = useFormik({ initialValues: { username: '', password: '', }, validationSchema: schema, - onSubmit: async (values) => { - await api.post('/authorization', { + onSubmit: async (values): Promise => { + return await api.post('/authorization', { username: values.username, password: values.password }).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 }) }, }) diff --git a/src/types.ts b/src/types.ts index 7861af8..f019fca 100644 --- a/src/types.ts +++ b/src/types.ts @@ -6,10 +6,16 @@ export interface AuctionItem { status: string } -type socialName = 'faceBook' | 'instagram' | 'whatsapp' - export type socialType = { name: socialName, img: string, href: string } + +export type AuthResponsePayload = { + accessToken: string + refreshToken: string +} | +string[] + +export type socialName = 'faceBook' | 'instagram' | 'whatsapp'