base commit
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { useState } from 'react'
|
||||
import { useEffect, useState } from 'react'
|
||||
import Alert from '@mui/material/Alert'
|
||||
import Box from '@mui/material/Box'
|
||||
import Button from '@mui/material/Button'
|
||||
@@ -8,10 +8,12 @@ import TextField from '@mui/material/TextField'
|
||||
import Typography from '@mui/material/Typography'
|
||||
import { useMutation } from '@tanstack/react-query'
|
||||
import { useForm } from 'react-hook-form'
|
||||
import { useUnit } from 'effector-react'
|
||||
import { useNavigate } from 'react-router-dom'
|
||||
import { apiClient } from '@/shared/api/client'
|
||||
import { tokenSet } from '@/shared/model/auth'
|
||||
import { $user, tokenSet } from '@/shared/model/auth'
|
||||
|
||||
type AuthResponse = { token: string; user: { id: string; email: string } }
|
||||
type AuthResponse = { token: string; user: { id: string; email: string; name?: string | null; phone?: string | null } }
|
||||
|
||||
function getApiErrorMessage(err: unknown): string | null {
|
||||
if (!err || typeof err !== 'object') return null
|
||||
@@ -24,6 +26,8 @@ function getApiErrorMessage(err: unknown): string | null {
|
||||
|
||||
export function AuthPage() {
|
||||
const [message, setMessage] = useState<string | null>(null)
|
||||
const navigate = useNavigate()
|
||||
const user = useUnit($user)
|
||||
const { register, watch } = useForm<{
|
||||
email: string
|
||||
code: string
|
||||
@@ -37,6 +41,10 @@ export function AuthPage() {
|
||||
const code = watch('code')
|
||||
const password = watch('password')
|
||||
|
||||
useEffect(() => {
|
||||
if (user) navigate('/', { replace: true })
|
||||
}, [navigate, user])
|
||||
|
||||
const requestCode = useMutation({
|
||||
mutationFn: async () => {
|
||||
await apiClient.post('auth/request-code', { email })
|
||||
@@ -49,6 +57,7 @@ export function AuthPage() {
|
||||
const { data } = await apiClient.post<AuthResponse>('auth/verify-code', { email, code })
|
||||
tokenSet(data.token)
|
||||
setMessage(`Вход выполнен: ${data.user.email}`)
|
||||
navigate('/', { replace: true })
|
||||
},
|
||||
})
|
||||
|
||||
@@ -57,6 +66,7 @@ export function AuthPage() {
|
||||
const { data } = await apiClient.post<AuthResponse>('auth/register', { email, password })
|
||||
tokenSet(data.token)
|
||||
setMessage(`Регистрация выполнена: ${data.user.email}`)
|
||||
navigate('/', { replace: true })
|
||||
},
|
||||
})
|
||||
|
||||
@@ -65,6 +75,7 @@ export function AuthPage() {
|
||||
const { data } = await apiClient.post<AuthResponse>('auth/login', { email, password })
|
||||
tokenSet(data.token)
|
||||
setMessage(`Вход выполнен: ${data.user.email}`)
|
||||
navigate('/', { replace: true })
|
||||
},
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user