Skip to content

Commit 200a6b2

Browse files
use Router instead of tanstack router
1 parent 1676a8c commit 200a6b2

File tree

4 files changed

+64
-66
lines changed

4 files changed

+64
-66
lines changed

frontend/src/components/auth/SignUpForm.tsx

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@ import Input from "@/components/form/input/InputField";
33
import Label from "@/components/form/Label";
44
import { ChevronLeftIcon, EyeCloseIcon, EyeIcon } from "@/icons";
55
import Link from "next/link";
6-
import React, { useState } from "react";
6+
import React, { useEffect, useState } from "react";
77
import { namePattern, emailPattern, passwordRules } from "@/utils";
8-
import { useForm } from "react-hook-form";
9-
import { useSignup } from "@/hooks/useSignup";
8+
import { type SubmitHandler, useForm } from "react-hook-form"
9+
import useAuth, { isLoggedIn } from "@/hooks/useAuth";
10+
import type { UserRegister } from "@/client";
1011
import SpinnerButton from "../ui/button/SpinnerButton";
12+
import { useRouter } from "next/navigation";
13+
1114

1215

1316
type FormData = {
@@ -19,34 +22,33 @@ type FormData = {
1922

2023
export default function SignUpForm() {
2124
const [showPassword, setShowPassword] = useState(false);
22-
const signupMutation = useSignup();
25+
26+
const { signUpMutation } = useAuth();
27+
const router = useRouter();
28+
29+
30+
useEffect(() => {
31+
if (isLoggedIn()) {
32+
router.push("/");
33+
}
34+
}, [router])
35+
2336

2437
const {
2538
register,
2639
handleSubmit,
27-
formState: { errors },
40+
formState: { errors, isSubmitting },
2841
} = useForm<FormData>({
2942
mode: "onChange"
3043
});
31-
const onSubmit = (data: FormData) => {
32-
console.log("Form data:", data);
33-
signupMutation.mutate(
34-
{
35-
requestBody: {
36-
full_name: data.firstName + " " + data.lastName,
37-
password: data.password,
38-
email: data.email,
39-
},
40-
},
41-
{
42-
onSuccess: (res) => {
43-
console.log("Signup success:", res);
44-
},
45-
onError: (err) => {
46-
console.error("Signup failed:", err);
47-
},
48-
}
49-
);
44+
const onSubmit: SubmitHandler<FormData> = (data) => {
45+
const payload: UserRegister = {
46+
full_name: `${data.firstName} ${data.lastName}`,
47+
email: data.email,
48+
password: data.password,
49+
};
50+
51+
signUpMutation.mutate(payload);
5052
};
5153

5254
return (
@@ -149,8 +151,8 @@ export default function SignUpForm() {
149151
<div className="sm:col-span-2">
150152
<SpinnerButton
151153
className="flex items-center justify-center w-full px-4 py-3 text-sm font-medium text-white transition rounded-lg bg-brand-500 shadow-theme-xs hover:bg-brand-600"
152-
disabled={signupMutation.isPending}
153-
loading={signupMutation.isPending}
154+
disabled={!!errors.firstName || !!errors.lastName || !!errors.email || !!errors.password}
155+
loading={isSubmitting}
154156
>
155157
Sign Up
156158
</SpinnerButton>

frontend/src/hooks/useAuth.ts

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"
2-
import { useNavigate } from "@tanstack/react-router"
3-
import { useState } from "react"
1+
"use client";
2+
3+
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
4+
import { useRouter } from "next/navigation";
5+
import { useState } from "react";
46

57
import {
68
type Body_login_login_access_token as AccessToken,
@@ -9,59 +11,63 @@ import {
911
type UserPublic,
1012
type UserRegister,
1113
UsersService,
12-
} from "@/client"
13-
import { handleError } from "@/utils"
14+
} from "@/client";
15+
import { handleError } from "@/utils";
1416

1517
const isLoggedIn = () => {
16-
return localStorage.getItem("access_token") !== null
17-
}
18+
return localStorage.getItem("access_token") !== null;
19+
};
1820

1921
const useAuth = () => {
20-
const [error, setError] = useState<string | null>(null)
21-
const navigate = useNavigate()
22-
const queryClient = useQueryClient()
22+
const [error, setError] = useState<string | null>(null);
23+
const router = useRouter();
24+
const queryClient = useQueryClient();
25+
2326
const { data: user } = useQuery<UserPublic | null, Error>({
2427
queryKey: ["currentUser"],
2528
queryFn: UsersService.readUserMe,
2629
enabled: isLoggedIn(),
27-
})
30+
});
2831

2932
const signUpMutation = useMutation({
3033
mutationFn: (data: UserRegister) =>
3134
UsersService.registerUser({ requestBody: data }),
32-
3335
onSuccess: () => {
34-
navigate({ to: "/login" })
36+
router.push("/signin");
3537
},
3638
onError: (err: ApiError) => {
37-
handleError(err)
39+
handleError(err);
40+
setError(err.message || "Signup failed");
3841
},
3942
onSettled: () => {
40-
queryClient.invalidateQueries({ queryKey: ["users"] })
43+
queryClient.invalidateQueries({ queryKey: ["users"] });
4144
},
42-
})
45+
});
4346

4447
const login = async (data: AccessToken) => {
4548
const response = await LoginService.loginAccessToken({
4649
formData: data,
47-
})
48-
localStorage.setItem("access_token", response.access_token)
49-
}
50+
});
51+
localStorage.setItem("access_token", response.access_token);
52+
queryClient.invalidateQueries({ queryKey: ["currentUser"] });
53+
};
5054

5155
const loginMutation = useMutation({
5256
mutationFn: login,
5357
onSuccess: () => {
54-
navigate({ to: "/" })
58+
router.push("/");
5559
},
5660
onError: (err: ApiError) => {
57-
handleError(err)
61+
handleError(err);
62+
setError(err.message || "Login failed");
5863
},
59-
})
64+
});
6065

6166
const logout = () => {
62-
localStorage.removeItem("access_token")
63-
navigate({ to: "/login" })
64-
}
67+
localStorage.removeItem("access_token");
68+
queryClient.invalidateQueries({ queryKey: ["currentUser"] });
69+
router.push("/signin");
70+
};
6571

6672
return {
6773
signUpMutation,
@@ -70,8 +76,8 @@ const useAuth = () => {
7076
user,
7177
error,
7278
resetError: () => setError(null),
73-
}
74-
}
79+
};
80+
};
7581

76-
export { isLoggedIn }
77-
export default useAuth
82+
export { isLoggedIn };
83+
export default useAuth;

frontend/src/hooks/useSignup.ts

Lines changed: 0 additions & 10 deletions
This file was deleted.

frontend/src/layout/StaticHeader.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ const StaticHeader: React.FC = () => {
3333
{/* Right Side Actions */}
3434
<div className="flex items-center gap-3">
3535
<Link
36-
href="/login"
36+
href="/signin"
3737
className="rounded-md border border-gray-300 px-3 py-1.5 text-sm font-medium text-gray-700 hover:bg-gray-100 dark:border-gray-700 dark:text-gray-200 dark:hover:bg-gray-800"
3838
>
3939
Log in

0 commit comments

Comments
 (0)