Skip to content

Commit 8b0c94c

Browse files
committed
Update password validation criteria
1 parent 3c0263c commit 8b0c94c

File tree

1 file changed

+71
-22
lines changed

1 file changed

+71
-22
lines changed

frontend/src/app/(user)/register/components/Register.tsx

Lines changed: 71 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,14 @@
22

33
import { register } from "@/api/user";
44
import { Button } from "@/components/ui/button";
5-
import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from "@/components/ui/form";
5+
import {
6+
Form,
7+
FormControl,
8+
FormField,
9+
FormItem,
10+
FormLabel,
11+
FormMessage,
12+
} from "@/components/ui/form";
613
import { Input } from "@/components/ui/input";
714
import { zodResolver } from "@hookform/resolvers/zod";
815
import { useForm } from "react-hook-form";
@@ -13,14 +20,22 @@ import { useRouter } from "next/navigation";
1320
import { useEffect, useState } from "react";
1421

1522
const formSchema = z.object({
16-
username: z.string()
23+
username: z
24+
.string()
1725
.min(5, "Username must be at least 5 characters")
1826
.max(20, "Username must be at most 20 characters"),
19-
email: z.string()
20-
.email("Invalid email"),
21-
password: z.string()
27+
email: z.string().email("Invalid email"),
28+
password: z
29+
.string()
2230
.min(8, "Password must be at least 8 characters")
23-
.max(100, "Password must be at most 100 characters"),
31+
.max(100, "Password must be at most 100 characters")
32+
.regex(/[a-z]/, "Password must contain at least one lowercase letter")
33+
.regex(/[A-Z]/, "Password must contain at least one uppercase letter")
34+
.regex(/\d/, "Password must contain at least one number")
35+
.regex(
36+
/[@$!%*?&]/,
37+
"Password must contain at least one special character (@, $, !, %, *, ?, &)"
38+
),
2439
});
2540

2641
const Register = () => {
@@ -41,15 +56,19 @@ const Register = () => {
4156
});
4257

4358
const onSubmit = async (data: z.infer<typeof formSchema>) => {
44-
const registerIsSuccessful = await register(data.email, data.password, data.username);
59+
const registerIsSuccessful = await register(
60+
data.email,
61+
data.password,
62+
data.username
63+
);
4564
if (!registerIsSuccessful) return;
4665

4766
// redirect to /login
4867
Swal.fire({
4968
title: "Account created!",
5069
text: "You can now login to your account.",
5170
icon: "success",
52-
confirmButtonText: "Login"
71+
confirmButtonText: "Login",
5372
}).then(() => {
5473
if (!mounted) return;
5574
router.push("/login");
@@ -60,25 +79,37 @@ const Register = () => {
6079

6180
return (
6281
<>
63-
<Navbar/>
82+
<Navbar />
6483
<div className="max-w-xl mx-auto my-10 p-2">
6584
<h1 className="text-white font-extrabold text-h1">Register</h1>
6685
<p className="text-primary-300 text-lg">
67-
Register to our platform to access its features! Have an account? <a href="/login" className="text-yellow-500 hover:underline">Login here!</a>
86+
Register to our platform to access its features! Have an account?{" "}
87+
<a href="/login" className="text-yellow-500 hover:underline">
88+
Login here!
89+
</a>
6890
</p>
6991
<Form {...form}>
70-
<form className="my-10 grid gap-4" onSubmit={form.handleSubmit(onSubmit)}>
92+
<form
93+
className="my-10 grid gap-4"
94+
onSubmit={form.handleSubmit(onSubmit)}
95+
>
7196
<FormField
7297
control={form.control}
7398
name="username"
7499
render={({ field }) => (
75100
<FormItem>
76-
<FormLabel className="text-yellow-500 text-lg">USERNAME</FormLabel>
101+
<FormLabel className="text-yellow-500 text-lg">
102+
USERNAME
103+
</FormLabel>
77104
<FormControl>
78-
<Input placeholder="username" {...field} className="focus:border-yellow-500 text-white"/>
105+
<Input
106+
placeholder="username"
107+
{...field}
108+
className="focus:border-yellow-500 text-white"
109+
/>
79110
</FormControl>
80111
{/* <FormDescription>This is your public display name.</FormDescription> */}
81-
<FormMessage/>
112+
<FormMessage />
82113
</FormItem>
83114
)}
84115
/>
@@ -87,12 +118,18 @@ const Register = () => {
87118
name="email"
88119
render={({ field }) => (
89120
<FormItem>
90-
<FormLabel className="text-yellow-500 text-lg">EMAIL</FormLabel>
121+
<FormLabel className="text-yellow-500 text-lg">
122+
EMAIL
123+
</FormLabel>
91124
<FormControl>
92-
<Input placeholder="email" {...field} className="focus:border-yellow-500 text-white"/>
125+
<Input
126+
placeholder="email"
127+
{...field}
128+
className="focus:border-yellow-500 text-white"
129+
/>
93130
</FormControl>
94131
{/* <FormDescription>This is your public display name.</FormDescription> */}
95-
<FormMessage/>
132+
<FormMessage />
96133
</FormItem>
97134
)}
98135
/>
@@ -101,21 +138,33 @@ const Register = () => {
101138
name="password"
102139
render={({ field }) => (
103140
<FormItem>
104-
<FormLabel className="text-yellow-500 text-lg">PASSWORD</FormLabel>
141+
<FormLabel className="text-yellow-500 text-lg">
142+
PASSWORD
143+
</FormLabel>
105144
<FormControl>
106-
<Input placeholder="password" type="password" {...field} className="focus:border-yellow-500 text-white"/>
145+
<Input
146+
placeholder="password"
147+
type="password"
148+
{...field}
149+
className="focus:border-yellow-500 text-white"
150+
/>
107151
</FormControl>
108152
{/* <FormDescription>This is your public display name.</FormDescription> */}
109-
<FormMessage/>
153+
<FormMessage />
110154
</FormItem>
111155
)}
112156
/>
113-
<Button type="submit" className="bg-yellow-500 hover:bg-yellow-300 px-4 py-2 my-2 rounded-md text-black">Register</Button>
157+
<Button
158+
type="submit"
159+
className="bg-yellow-500 hover:bg-yellow-300 px-4 py-2 my-2 rounded-md text-black"
160+
>
161+
Register
162+
</Button>
114163
</form>
115164
</Form>
116165
</div>
117166
</>
118167
);
119168
};
120169

121-
export default Register;
170+
export default Register;

0 commit comments

Comments
 (0)