Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 50e149e

Browse files
committedJan 9, 2025·
fix signin
1 parent dde6e22 commit 50e149e

File tree

2 files changed

+78
-66
lines changed

2 files changed

+78
-66
lines changed
 
+73-66
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,50 @@
1-
"use client";
1+
"use client"
22

3-
import React, { useState } from "react";
4-
import { useRouter } from "next/navigation";
5-
import { Input } from "@/components/ui/input";
6-
import { Label } from "@/components/ui/label";
7-
import { Alert, AlertDescription } from "@/components/ui/alert";
3+
import React from "react"
4+
import { useRouter } from "next/navigation"
5+
import { zodResolver } from "@hookform/resolvers/zod"
6+
import { useForm } from "react-hook-form"
7+
import * as z from "zod"
8+
import {
9+
Form,
10+
FormControl,
11+
FormField,
12+
FormItem,
13+
FormLabel,
14+
FormMessage,
15+
} from "@/components/ui/form"
16+
import { Input } from "@/components/ui/input"
17+
import { Alert, AlertDescription } from "@/components/ui/alert"
818
import {
919
Card,
1020
CardContent,
1121
CardDescription,
1222
CardFooter,
1323
CardHeader,
1424
CardTitle,
15-
} from "@/components/ui/card";
16-
import { useAuthStore } from "@/store/AuthStore/useAuthStore";
17-
import LoadingButton from "../Buttons/LoadingButton";
18-
import AuthBottom from "./AuthBottom";
25+
} from "@/components/ui/card"
26+
import { useAuthStore } from "@/store/AuthStore/useAuthStore"
27+
import LoadingButton from "../Buttons/LoadingButton"
28+
import AuthBottom from "./AuthBottom"
29+
import { signinSchema } from "@/validations/validation"
1930

20-
interface FormData {
21-
email: string;
22-
password: string;
23-
}
31+
type SigninFormValues = z.infer<typeof signinSchema>
2432

2533
export default function SigninForm() {
26-
const { isSigningIn, signin, signinError } = useAuthStore();
27-
const router = useRouter();
28-
const [formData, setFormData] = useState<FormData>({
29-
email: "",
30-
password: "",
31-
});
34+
const { isSigningIn, signin, signinError } = useAuthStore()
35+
const router = useRouter()
3236

33-
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
34-
const { name, value } = e.target;
35-
setFormData((prev) => ({ ...prev, [name]: value }));
36-
};
37+
const form = useForm<SigninFormValues>({
38+
resolver: zodResolver(signinSchema),
39+
defaultValues: {
40+
email: "",
41+
password: "",
42+
},
43+
})
3744

38-
const handleSubmit = async (e: React.FormEvent) => {
39-
e.preventDefault();
40-
signin(formData, router);
41-
};
45+
const onSubmit = (data: SigninFormValues) => {
46+
signin(data, router)
47+
}
4248

4349
return (
4450
<main className="flex min-h-screen items-center justify-center px-4">
@@ -50,46 +56,47 @@ export default function SigninForm() {
5056
</CardDescription>
5157
</CardHeader>
5258
<CardContent>
53-
<form onSubmit={handleSubmit} className="space-y-4">
54-
<div className="space-y-2">
55-
<Label htmlFor="email">Email</Label>
56-
<Input
57-
id="email"
59+
<Form {...form}>
60+
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-4">
61+
<FormField
62+
control={form.control}
5863
name="email"
59-
type="email"
60-
placeholder="you@example.com"
61-
value={formData.email}
62-
onChange={handleChange}
63-
required
64+
render={({ field }) => (
65+
<FormItem>
66+
<FormLabel>Email</FormLabel>
67+
<FormControl>
68+
<Input placeholder="you@example.com" {...field} />
69+
</FormControl>
70+
<FormMessage />
71+
</FormItem>
72+
)}
6473
/>
65-
</div>
66-
67-
<div className="space-y-2">
68-
<Label htmlFor="password">Password</Label>
69-
<Input
70-
id="password"
74+
<FormField
75+
control={form.control}
7176
name="password"
72-
type="password"
73-
placeholder="••••••••"
74-
value={formData.password}
75-
onChange={handleChange}
76-
required
77+
render={({ field }) => (
78+
<FormItem>
79+
<FormLabel>Password</FormLabel>
80+
<FormControl>
81+
<Input type="password" placeholder="••••••••" {...field} />
82+
</FormControl>
83+
<FormMessage />
84+
</FormItem>
85+
)}
7786
/>
78-
</div>
79-
80-
{signinError && (
81-
<Alert variant="destructive">
82-
<AlertDescription>{signinError}</AlertDescription>
83-
</Alert>
84-
)}
85-
86-
<LoadingButton
87-
loading={isSigningIn}
88-
loadingTitle="Signing in"
89-
title="Signin"
90-
type="submit"
91-
/>
92-
</form>
87+
{signinError && (
88+
<Alert variant="destructive">
89+
<AlertDescription>{signinError}</AlertDescription>
90+
</Alert>
91+
)}
92+
<LoadingButton
93+
loading={isSigningIn}
94+
loadingTitle="Signing in"
95+
title="Sign in"
96+
type="submit"
97+
/>
98+
</form>
99+
</Form>
93100
</CardContent>
94101
<CardFooter className="flex justify-center">
95102
<AuthBottom
@@ -100,5 +107,5 @@ export default function SigninForm() {
100107
</CardFooter>
101108
</Card>
102109
</main>
103-
);
104-
}
110+
)
111+
}

‎validations/validation.ts

+5
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,9 @@ export const signupSchema = z.object({
2525
.nonempty({ message: "Please select your gender " }),
2626
});
2727

28+
export const signinSchema = z.object({
29+
email: z.string().email({ message: "Please provide a valid email address." }),
30+
password: z.string().nonempty({ message: "Password is required." }),
31+
});
32+
2833

0 commit comments

Comments
 (0)
Please sign in to comment.