Skip to content

Commit 504f7ca

Browse files
committed
feat: add profile check and redirect for logged-in users on login and registration pages
1 parent ff59b02 commit 504f7ca

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

frontend/app/login/page.tsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,23 @@ import { Form } from "@heroui/form";
55
import { Input } from "@heroui/input";
66
import { Button } from "@heroui/button";
77
import "react-toastify/dist/ReactToastify.css";
8-
import React, { useState } from "react";
9-
import { login } from "../functions/UserAPI";
8+
import React, { useState, useEffect } from "react";
9+
import { login, useGetProfile } from "../functions/UserAPI";
1010
import { PatreonLoginButton } from "@/components/PatreonLoginButton";
11+
import { useRouter } from "next/navigation";
1112

1213
export default function LoginPage() {
1314
const [isLoading, setIsLoading] = useState(false);
1415
const [error, setError] = useState<string | null>(null);
16+
const { data: profile, isLoading: isProfileLoading } = useGetProfile();
17+
const router = useRouter();
18+
19+
// Redirect to home if user is already logged in
20+
useEffect(() => {
21+
if (!isProfileLoading && profile?.user) {
22+
router.push("/");
23+
}
24+
}, [profile, isProfileLoading, router]);
1525

1626
async function handleLogin(formData: FormData) {
1727
try {

frontend/app/register/page.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import { Card, CardHeader, CardBody } from "@heroui/card";
44
import { Form } from "@heroui/form";
55
import { Input } from "@heroui/input";
66
import { Button } from "@heroui/button";
7-
import React, { useState } from "react";
7+
import React, { useState, useEffect } from "react";
88
import { useRouter } from "next/navigation";
9-
import { register } from "../functions/UserAPI";
9+
import { register, useGetProfile } from "../functions/UserAPI";
1010
import { RegisterData } from "../types/User";
1111

1212
export default function RegisterPage() {
@@ -25,6 +25,14 @@ export default function RegisterPage() {
2525
confirmPassword?: string;
2626
}>({});
2727
const router = useRouter();
28+
const { data: profile, isLoading: isProfileLoading } = useGetProfile();
29+
30+
// Redirect to home if user is already logged in
31+
useEffect(() => {
32+
if (!isProfileLoading && profile?.user) {
33+
router.push("/");
34+
}
35+
}, [profile, isProfileLoading, router]);
2836

2937
const validatePassword = (password: string): string | null => {
3038
if (password.length < 8) {

0 commit comments

Comments
 (0)