From 8cca16a0f9bb32f7549c846a90ed9d12921cac28 Mon Sep 17 00:00:00 2001 From: me Date: Thu, 7 May 2026 04:59:29 +0530 Subject: [PATCH] refactor: rename PublicRoute with AutoRedirectRoute --- frontend/src/components/RouteGuards.test.tsx | 200 +++++++++---------- frontend/src/components/RouteGuards.tsx | 30 +-- 2 files changed, 115 insertions(+), 115 deletions(-) diff --git a/frontend/src/components/RouteGuards.test.tsx b/frontend/src/components/RouteGuards.test.tsx index c0d59a7..946d1bb 100644 --- a/frontend/src/components/RouteGuards.test.tsx +++ b/frontend/src/components/RouteGuards.test.tsx @@ -3,125 +3,125 @@ import { MemoryRouter, Route, Routes } from "react-router-dom"; import { beforeEach, describe, expect, it } from "vitest"; import { mockUser } from "../../test/fixtures/user.fixture"; import { useAuthStore } from "../store/useAuthStore"; -import { ProtectedRoute, PublicRoute } from "./RouteGuards"; +import { ProtectedRoute, AutoRedirectRoute } from "./RouteGuards"; function renderGuard(ui: React.ReactNode, mountPath: "/protected" | "/public") { - return render( - - - Login Page} /> - Drawer Page} /> - - - - , - ); + return render( + + + Login Page} /> + Drawer Page} /> + + + + , + ); } beforeEach(() => { - useAuthStore.setState({ - accessToken: null, - user: null, - isInitializing: true, - }); + useAuthStore.setState({ + accessToken: null, + user: null, + isInitializing: true, + }); }); describe("ProtectedRoute", () => { - it("should show SplashScreen while auth is initializing", () => { - useAuthStore.setState({ - isInitializing: true, - accessToken: null, - user: null, + it("should show SplashScreen while auth is initializing", () => { + useAuthStore.setState({ + isInitializing: true, + accessToken: null, + user: null, + }); + renderGuard( + +
Secret
+
, + "/protected", + ); + + expect(screen.getByText(/Unsealing/i)).toBeInTheDocument(); + expect(screen.queryByText("Secret")).not.toBeInTheDocument(); }); - renderGuard( - -
Secret
-
, - "/protected", - ); - expect(screen.getByText(/Unsealing/i)).toBeInTheDocument(); - expect(screen.queryByText("Secret")).not.toBeInTheDocument(); - }); - - it("should redirect unauthenticated users to /login", () => { - useAuthStore.setState({ - isInitializing: false, - accessToken: null, - user: null, + it("should redirect unauthenticated users to /login", () => { + useAuthStore.setState({ + isInitializing: false, + accessToken: null, + user: null, + }); + renderGuard( + +
Secret
+
, + "/protected", + ); + expect(screen.getByText("Login Page")).toBeInTheDocument(); + expect(screen.queryByText("Secret")).not.toBeInTheDocument(); }); - renderGuard( - -
Secret
-
, - "/protected", - ); - expect(screen.getByText("Login Page")).toBeInTheDocument(); - expect(screen.queryByText("Secret")).not.toBeInTheDocument(); - }); - it("should render page for authenticated users", () => { - useAuthStore.setState({ - isInitializing: false, - accessToken: "token", - user: mockUser, + it("should render page for authenticated users", () => { + useAuthStore.setState({ + isInitializing: false, + accessToken: "token", + user: mockUser, + }); + renderGuard( + +
Secret
+
, + "/protected", + ); + + expect(screen.getByText("Secret")).toBeInTheDocument(); }); - renderGuard( - -
Secret
-
, - "/protected", - ); - - expect(screen.getByText("Secret")).toBeInTheDocument(); - }); }); describe("PublicRoute", () => { - it("should show SplashScreen while auth is initializing", () => { - useAuthStore.setState({ - isInitializing: true, - accessToken: null, - user: null, + it("should show SplashScreen while auth is initializing", () => { + useAuthStore.setState({ + isInitializing: true, + accessToken: null, + user: null, + }); + renderGuard( + +
Login Page
+
, + "/public", + ); + expect(screen.getByText(/Unsealing/i)).toBeInTheDocument(); + expect(screen.queryByText("Login Page")).not.toBeInTheDocument(); }); - renderGuard( - -
Login Page
-
, - "/public", - ); - expect(screen.getByText(/Unsealing/i)).toBeInTheDocument(); - expect(screen.queryByText("Login Page")).not.toBeInTheDocument(); - }); - it("should redirect authenticated users to /drawer", () => { - useAuthStore.setState({ - isInitializing: false, - accessToken: "token", - user: mockUser, + it("should redirect authenticated users to /drawer", () => { + useAuthStore.setState({ + isInitializing: false, + accessToken: "token", + user: mockUser, + }); + renderGuard( + +
Login Form
+
, + "/public", + ); + expect(screen.getByText("Drawer Page")).toBeInTheDocument(); + expect(screen.queryByText("Login Form")).not.toBeInTheDocument(); }); - renderGuard( - -
Login Form
-
, - "/public", - ); - expect(screen.getByText("Drawer Page")).toBeInTheDocument(); - expect(screen.queryByText("Login Form")).not.toBeInTheDocument(); - }); - it("should render page for unauthenticated users", () => { - useAuthStore.setState({ - isInitializing: false, - accessToken: null, - user: null, + it("should render page for unauthenticated users", () => { + useAuthStore.setState({ + isInitializing: false, + accessToken: null, + user: null, + }); + renderGuard( + +
Login Form
+
, + "/public", + ); + expect(screen.getByText("Login Form")).toBeInTheDocument(); }); - renderGuard( - -
Login Form
-
, - "/public", - ); - expect(screen.getByText("Login Form")).toBeInTheDocument(); - }); }); diff --git a/frontend/src/components/RouteGuards.tsx b/frontend/src/components/RouteGuards.tsx index 5c4d53e..59e9b23 100644 --- a/frontend/src/components/RouteGuards.tsx +++ b/frontend/src/components/RouteGuards.tsx @@ -9,30 +9,30 @@ import SplashScreen from "./SplashScreen"; * state so the Login component can link them back after sign-in */ export function ProtectedRoute({ children }: { children: React.ReactNode }) { - const { isAuthenticated, isInitializing } = useAuth(); - const location = useLocation(); + const { isAuthenticated, isInitializing } = useAuth(); + const location = useLocation(); - if (isInitializing) return ; + if (isInitializing) return ; - if (!isAuthenticated) { - return ; - } + if (!isAuthenticated) { + return ; + } - return <>{children}; + return <>{children}; } /** - * Public - auth route guard. + * Auto-redirect - auth route guard. * If authenticated, redirect all the auth related flows to the drawer */ -export function PublicRoute({ children }: { children: React.ReactNode }) { - const { isAuthenticated, isInitializing } = useAuth(); +export function AutoRedirectRoute({ children }: { children: React.ReactNode }) { + const { isAuthenticated, isInitializing } = useAuth(); - if (isInitializing) return ; + if (isInitializing) return ; - if (isAuthenticated) { - return ; - } + if (isAuthenticated) { + return ; + } - return <>{children}; + return <>{children}; }