mirror of
https://github.com/Monadical-SAS/reflector.git
synced 2025-12-20 20:29:06 +00:00
refactor: remove redundant client-side AuthGuard
The authentication is already properly handled by Next.js middleware in middleware.ts with LOGIN_REQUIRED_PAGES. The middleware approach is superior as it: - Provides server-side protection before page loads - Prevents flash of unauthorized content - Centralizes auth logic in one place - Better performance (no client-side JS needed) Keep the API hooks conditional to prevent 401 errors before token is ready.
This commit is contained in:
@@ -1,70 +0,0 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect } from "react";
|
||||
import { useRouter, usePathname } from "next/navigation";
|
||||
import { signIn } from "next-auth/react";
|
||||
import useSessionStatus from "../lib/useSessionStatus";
|
||||
import { Flex, Spinner } from "@chakra-ui/react";
|
||||
|
||||
interface AuthGuardProps {
|
||||
children: React.ReactNode;
|
||||
requireAuth?: boolean;
|
||||
}
|
||||
|
||||
// Routes that should be accessible without authentication
|
||||
const PUBLIC_ROUTES = ["/transcripts/new"];
|
||||
|
||||
export default function AuthGuard({
|
||||
children,
|
||||
requireAuth = true,
|
||||
}: AuthGuardProps) {
|
||||
const { isAuthenticated, isLoading, status } = useSessionStatus();
|
||||
const router = useRouter();
|
||||
const pathname = usePathname();
|
||||
|
||||
// Check if current route is public
|
||||
const isPublicRoute = PUBLIC_ROUTES.some((route) =>
|
||||
pathname.startsWith(route),
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
// Don't require auth for public routes
|
||||
if (isPublicRoute) return;
|
||||
|
||||
// Only redirect if we're sure the user is not authenticated and auth is required
|
||||
if (!isLoading && requireAuth && status === "unauthenticated") {
|
||||
// Instead of redirecting to /login, trigger NextAuth signIn
|
||||
signIn("authentik");
|
||||
}
|
||||
}, [isLoading, requireAuth, status, isPublicRoute]);
|
||||
|
||||
// For public routes, always show content
|
||||
if (isPublicRoute) {
|
||||
return <>{children}</>;
|
||||
}
|
||||
|
||||
// Show loading spinner while checking authentication
|
||||
if (
|
||||
isLoading ||
|
||||
(requireAuth && !isAuthenticated && status !== "unauthenticated")
|
||||
) {
|
||||
return (
|
||||
<Flex
|
||||
flexDir="column"
|
||||
alignItems="center"
|
||||
justifyContent="center"
|
||||
h="100%"
|
||||
>
|
||||
<Spinner size="xl" />
|
||||
</Flex>
|
||||
);
|
||||
}
|
||||
|
||||
// If authentication is not required or user is authenticated, show content
|
||||
if (!requireAuth || isAuthenticated) {
|
||||
return <>{children}</>;
|
||||
}
|
||||
|
||||
// Don't render anything while redirecting
|
||||
return null;
|
||||
}
|
||||
@@ -6,7 +6,6 @@ import About from "../(aboutAndPrivacy)/about";
|
||||
import Privacy from "../(aboutAndPrivacy)/privacy";
|
||||
import UserInfo from "../(auth)/userInfo";
|
||||
import { RECORD_A_MEETING_URL } from "../lib/constants";
|
||||
import AuthGuard from "./AuthGuard";
|
||||
|
||||
export default async function AppLayout({
|
||||
children,
|
||||
@@ -91,7 +90,7 @@ export default async function AppLayout({
|
||||
</div>
|
||||
</Flex>
|
||||
|
||||
<AuthGuard requireAuth={requireLogin}>{children}</AuthGuard>
|
||||
{children}
|
||||
</Container>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user