Skip to content

Commit 45f0c6e

Browse files
authored
Merge pull request #17 from LocalineServices/fix/authentication-check
Centralize Auth Redirect Logic in AuthLayout Component
2 parents 3f0a42d + b422e59 commit 45f0c6e

3 files changed

Lines changed: 43 additions & 40 deletions

File tree

src/app/(auth)/layout.tsx

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,55 @@
1+
"use client";
2+
13
import { Icons } from "@/components/icons";
24
import Link from "next/link";
35
import Image from "next/image";
6+
import { useRouter } from "next/navigation";
7+
import { useEffect, useState } from "react";
48

59
export default function AuthLayout({
610
children,
711
}: {
812
children: React.ReactNode;
913
}) {
14+
const router = useRouter();
15+
const [isChecking, setIsChecking] = useState(true);
16+
17+
// Redirect if already logged in
18+
useEffect(() => {
19+
async function checkAuth() {
20+
try {
21+
const response = await fetch('/api/users/me', {
22+
credentials: 'include',
23+
});
24+
25+
if (response.ok) {
26+
// User is already authenticated, redirect to projects
27+
router.push('/projects');
28+
return;
29+
}
30+
31+
setIsChecking(false);
32+
} catch {
33+
// User is not authenticated, stay on auth page
34+
setIsChecking(false);
35+
}
36+
}
37+
38+
checkAuth();
39+
}, [router]);
40+
41+
// Show loading state while checking authentication
42+
if (isChecking) {
43+
return (
44+
<div className="flex h-screen items-center justify-center">
45+
<div className="flex flex-col items-center gap-2">
46+
<div className="h-8 w-8 animate-spin rounded-full border-4 border-primary border-t-transparent" />
47+
<p className="text-sm text-muted-foreground">Verifying authentication...</p>
48+
</div>
49+
</div>
50+
);
51+
}
52+
1053
return (
1154
<div className="min-h-screen grid lg:grid-cols-2">
1255
{/* Left side - Branding */}

src/app/(auth)/login/page.tsx

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -34,26 +34,6 @@ export default function LoginPage() {
3434
checkConfig();
3535
}, []);
3636

37-
// Redirect if already logged in
38-
React.useEffect(() => {
39-
async function checkAuth() {
40-
try {
41-
const response = await fetch('/api/users/me', {
42-
credentials: 'include',
43-
});
44-
45-
if (response.ok) {
46-
// User is already authenticated, redirect to projects
47-
router.push('/projects');
48-
}
49-
} catch {
50-
// User is not authenticated, stay on login page
51-
}
52-
}
53-
54-
checkAuth();
55-
}, [router]);
56-
5737
async function onSubmit(event: React.FormEvent<HTMLFormElement>) {
5838
event.preventDefault();
5939
setIsLoading(true);

src/app/(auth)/signup/page.tsx

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -37,26 +37,6 @@ export default function SignupPage() {
3737
checkConfig();
3838
}, []);
3939

40-
// Redirect if already logged in
41-
React.useEffect(() => {
42-
async function checkAuth() {
43-
try {
44-
const response = await fetch('/api/users/me', {
45-
credentials: 'include',
46-
});
47-
48-
if (response.ok) {
49-
// User is already authenticated, redirect to projects
50-
router.push('/projects');
51-
}
52-
} catch {
53-
// User is not authenticated, stay on signup page
54-
}
55-
}
56-
57-
checkAuth();
58-
}, [router]);
59-
6040
async function onSubmit(event: React.FormEvent<HTMLFormElement>) {
6141
event.preventDefault();
6242
setIsLoading(true);

0 commit comments

Comments
 (0)