Регистрация, вход, базовый профиль
This commit is contained in:
@@ -0,0 +1,121 @@
|
||||
"use client"
|
||||
|
||||
import * as React from "react"
|
||||
import { useForm } from "react-hook-form"
|
||||
import { zodResolver } from "@hookform/resolvers/zod"
|
||||
import * as z from "zod"
|
||||
import { Loader2 } from "lucide-react"
|
||||
import { useRouter } from "next/navigation"
|
||||
|
||||
import { Button } from "@/components/ui/button"
|
||||
import { Input } from "@/components/ui/input"
|
||||
import {
|
||||
Form,
|
||||
FormControl,
|
||||
FormField,
|
||||
FormItem,
|
||||
FormLabel,
|
||||
FormMessage,
|
||||
} from "@/components/ui/form"
|
||||
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"
|
||||
import { useSessionStore } from "@/entities/session/store"
|
||||
|
||||
const registerSchema = z.object({
|
||||
phone: z.string().min(10, "Номер телефона должен содержать минимум 10 цифр"),
|
||||
password: z.string().min(6, "Пароль должен быть не менее 6 символов"),
|
||||
confirmPassword: z.string().min(6),
|
||||
}).refine((data) => data.password === data.confirmPassword, {
|
||||
message: "Пароли не совпадают",
|
||||
path: ["confirmPassword"],
|
||||
})
|
||||
|
||||
type RegisterValues = z.infer<typeof registerSchema>
|
||||
|
||||
export function RegisterForm() {
|
||||
const router = useRouter()
|
||||
const { register, isLoading } = useSessionStore()
|
||||
|
||||
const form = useForm<RegisterValues>({
|
||||
resolver: zodResolver(registerSchema),
|
||||
defaultValues: {
|
||||
phone: "",
|
||||
password: "",
|
||||
confirmPassword: "",
|
||||
},
|
||||
})
|
||||
|
||||
async function onSubmit(values: RegisterValues) {
|
||||
try {
|
||||
await register(values)
|
||||
router.push("/auth/login")
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
form.setError("root", { message: "Ошибка при регистрации. Возможно, номер уже занят." })
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<Card className="w-full max-w-md mx-auto">
|
||||
<CardHeader>
|
||||
<CardTitle className="text-2xl">Регистрация</CardTitle>
|
||||
<CardDescription>
|
||||
Создайте аккаунт, чтобы начать пользоваться сервисом.
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<Form {...form}>
|
||||
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-4">
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="phone"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>Телефон</FormLabel>
|
||||
<FormControl>
|
||||
<Input placeholder="+7 (999) 000-00-00" {...field} />
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="password"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>Пароль</FormLabel>
|
||||
<FormControl>
|
||||
<Input type="password" placeholder="******" {...field} />
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="confirmPassword"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>Подтвердите пароль</FormLabel>
|
||||
<FormControl>
|
||||
<Input type="password" placeholder="******" {...field} />
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
{form.formState.errors.root && (
|
||||
<p className="text-sm font-medium text-destructive">
|
||||
{form.formState.errors.root.message}
|
||||
</p>
|
||||
)}
|
||||
<Button type="submit" className="w-full bg-blue-600 hover:bg-blue-700" disabled={isLoading}>
|
||||
{isLoading && <Loader2 className="mr-2 h-4 w-4 animate-spin" />}
|
||||
Зарегистрироваться
|
||||
</Button>
|
||||
</form>
|
||||
</Form>
|
||||
</CardContent>
|
||||
</Card>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user