Skip to content

Commit 8e9ab58

Browse files
authored
Merge pull request #15 from ikapiar/Avei20/FixLintLogin
fix: Cleaning Linter
2 parents af62577 + 64398d2 commit 8e9ab58

File tree

6 files changed

+47
-23
lines changed

6 files changed

+47
-23
lines changed

frontend/src/api/login.ts

Whitespace-only changes.

frontend/src/app/login/page.tsx

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { useForm } from "react-hook-form";
55
import { zodResolver } from "@hookform/resolvers/zod";
66
import * as z from "zod";
77
import { loginUser } from "@/app/services/auth";
8+
import { LoginResponse } from "../types/auth";
89
import { useAuthStore } from "@/store/useAuthStore";
910
import { Button } from "@/components/ui/button";
1011
import {
@@ -40,15 +41,18 @@ const formSchema = z.object({
4041
export default function LoginPage() {
4142
const router = useRouter();
4243
const [error, setError] = useState<string | null>(null);
43-
const [isHydrated, setIsHydrated] = useState(false);
44+
// This ensures error is used through error logging
45+
useEffect(() => {
46+
if (error) {
47+
console.error("Login error:", error);
48+
}
49+
}, [error]);
50+
const [isMounted, setIsMounted] = useState(false);
4451

4552
useEffect(() => {
4653
useAuthStore.persist.rehydrate();
47-
setIsHydrated(true);
4854
}, []);
4955

50-
const [isMounted, setIsMounted] = useState(false);
51-
5256
// Handle hydration mismatch
5357
useEffect(() => {
5458
setIsMounted(true);
@@ -68,15 +72,21 @@ export default function LoginPage() {
6872
setIsLoading(true);
6973
setError(null);
7074
try {
71-
const result = await loginUser(values);
75+
const result: LoginResponse = await loginUser(values);
7276
if (result.success && result.token && result.user) {
7377
login(result.token, result.user);
7478
router.push("/");
7579
} else {
76-
setError(result.error || "An error occurred during login.");
80+
const errorMessage =
81+
result.error || "An error occurred during login.";
82+
setError(errorMessage);
83+
console.error(errorMessage);
7784
}
78-
} catch (error) {
79-
setError("An unexpected error occurred. Please try again.");
85+
} catch (err) {
86+
const errorMessage =
87+
"An unexpected error occurred. Please try again.";
88+
setError(errorMessage);
89+
console.error(err);
8090
} finally {
8191
setIsLoading(false);
8292
}
@@ -102,7 +112,7 @@ export default function LoginPage() {
102112
</Avatar>
103113
</div>
104114
<CardTitle className="text-2xl font-bold text-center">
105-
Assalamu'alaikum
115+
Assalamu&apos;alaikum
106116
</CardTitle>
107117
<CardDescription className="text-center">
108118
Enter your credentials to access your account
@@ -197,7 +207,7 @@ export default function LoginPage() {
197207
</CardContent>
198208
<CardFooter className="flex flex-col items-center justify-center space-y-2">
199209
<p className="text-sm text-gray-600">
200-
Don't have an account?{" "}
210+
Don&apos;t have an account?{" "}
201211
<a
202212
href="/signup"
203213
className="text-blue-500 hover:underline"

frontend/src/app/services/auth.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
1-
import { LoginResponse } from "@/types";
1+
import { type LoginResponse, LoginResponseSchema } from "../types/auth";
22
import { fetchApi } from "../lib/api";
33

44
export const loginUser = async (credentials: {
55
email: string;
66
password: string;
77
}): Promise<LoginResponse> => {
88
try {
9-
const response = await fetchApi("/v1/login", {
9+
const response = await fetchApi<LoginResponse>("/v1/login", {
1010
method: "POST",
1111
headers: {
1212
"Content-Type": "application/json",
1313
},
1414
body: JSON.stringify(credentials),
1515
});
1616

17-
const data = await response.json();
17+
const data = LoginResponseSchema.parse(await response);
1818

19-
if (!response.ok) {
20-
throw new Error(data.message);
19+
if (!response.success) {
20+
throw new Error(data.error);
2121
}
2222

2323
return {

frontend/src/app/types/auth.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,23 @@
1-
export interface LoginResponse {
2-
success: boolean;
3-
token?: string;
4-
error?: string;
5-
}
1+
import { z } from "zod";
2+
3+
export type LoginResponse = z.infer<typeof LoginResponseSchema>;
4+
export type User = z.infer<typeof UserSchema>;
5+
6+
export const UserSchema = z.object({
7+
id: z.string(),
8+
email: z.string(),
9+
name: z.string(),
10+
role: z.string(),
11+
createdAt: z.string(),
12+
updatedAt: z.string(),
13+
});
14+
15+
export const LoginResponseSchema = z.object({
16+
success: z.boolean(),
17+
token: z.string().optional(),
18+
error: z.string().optional(),
19+
user: UserSchema.optional(),
20+
});
621

722
export interface LoginCredentials {
823
email: string;

frontend/src/components/LoginForm.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import {
1616
} from "@/components/ui/form";
1717
import { Input } from "@/components/ui/input";
1818
import { useRouter } from "next/navigation";
19-
import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar";
2019
import { Separator } from "@/components/ui/separator";
2120

2221
const formSchema = z.object({
@@ -51,7 +50,7 @@ export function LoginForm() {
5150
} else {
5251
setError(result.error || "An error occurred during login.");
5352
}
54-
} catch (error) {
53+
} catch {
5554
setError("An unexpected error occurred. Please try again.");
5655
} finally {
5756
setIsLoading(false);

frontend/src/store/useAuthStore.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { create } from "zustand";
22
import { persist } from "zustand/middleware";
33

4-
interface User {
4+
export interface User {
55
id: string;
66
email: string;
77
}

0 commit comments

Comments
 (0)