Skip to content

Commit 369faab

Browse files
Update
1 parent 466c67f commit 369faab

File tree

2 files changed

+12
-21
lines changed

2 files changed

+12
-21
lines changed

Diff for: nextjs-training-frontend/src/actions/auth-actions.ts

+6-9
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@ import { hashUserPassword } from "@/lib/hash";
44
import { redirect } from "next/navigation";
55

66
interface FormState {
7-
errors: {
8-
email: string;
9-
password: string;
10-
};
7+
errors: string[];
118
}
129

1310
interface AuthFormData {
@@ -28,22 +25,22 @@ export const signup = async (prevState: FormState, formData: FormData) => {
2825
const email = formData.get("email") as string;
2926
const password = formData.get("password") as string;
3027

31-
let errors: { email: string; password: string } = { email: "", password: "" };
28+
let errors: string[] = [];
3229

3330
if (!email.includes("@")) {
34-
errors.email = "Please enter a valid email address.";
31+
errors.push("Please enter a valid email address.");
3532
}
3633

3734
if (password.trim().length < 8) {
38-
errors.password = "Password must be at least 8 characters long.";
35+
errors.push("Password must be at least 8 characters long.");
3936
}
4037

4138
const isUserExist = await checkUserExists(email);
4239
if (isUserExist) {
43-
errors.email = "It's seem like an account already exist";
40+
errors.push("It's seem like an account already exist.");
4441
}
4542

46-
if (errors.email || errors.password) {
43+
if (errors.length > 0) {
4744
return { errors };
4845
}
4946

Diff for: nextjs-training-frontend/src/components/auth-form.tsx

+6-12
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,11 @@ import { useCustomActionState } from "@/lib/custom";
55
import Link from "next/link";
66

77
interface FormState {
8-
errors: {
9-
email: string;
10-
password: string;
11-
};
8+
errors: string[];
129
}
1310

1411
const AuthForm: React.FC = () => {
15-
const initialState: FormState = { errors: { email: "", password: "" } };
12+
const initialState: FormState = { errors: [] };
1613
const [formState, formAction] = useCustomActionState<FormState>(
1714
signup,
1815
initialState
@@ -36,14 +33,11 @@ const AuthForm: React.FC = () => {
3633
<label htmlFor="password">Password</label>
3734
<input type="password" name="password" id="password" />
3835
</p>
39-
{formState.errors && (
36+
{formState.errors.length > 0 && (
4037
<ul id="form-errors">
41-
{Object.entries(formState.errors).map(([key, value]) => {
42-
if (typeof value === "string" && value) {
43-
return <li key={key}>{value}</li>;
44-
}
45-
return null;
46-
})}
38+
{formState.errors.map((error, index) => (
39+
<li key={index}>{error}</li>
40+
))}
4741
</ul>
4842
)}
4943
<p>

0 commit comments

Comments
 (0)