Skip to content

Commit b70e9b4

Browse files
author
Vadim Belov
committed
feat: enhance AppRoutes to include location-based public route matching
1 parent 1cee383 commit b70e9b4

1 file changed

Lines changed: 18 additions & 5 deletions

File tree

src/cotton.client/src/app/routes.tsx

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { RouteConfig } from "./types";
22
import { RequireAdmin, RequireAuth, useAuth } from "../features/auth";
33
import { useEffect } from "react";
4-
import { Routes, Route, Navigate, useParams } from "react-router-dom";
4+
import { Routes, Route, Navigate, useParams, useLocation, matchPath } from "react-router-dom";
55
import Loader from "../shared/ui/Loader";
66
import { useTranslation } from "react-i18next";
77

@@ -54,6 +54,7 @@ const publicRoutes: RouteConfig[] = [
5454

5555
export function AppRoutes() {
5656
const { t } = useTranslation(["login"]);
57+
const location = useLocation();
5758
const {
5859
hydrated,
5960
isInitializing,
@@ -63,14 +64,26 @@ export function AppRoutes() {
6364
ensureAuth,
6465
} = useAuth();
6566

67+
const isPublicRoute = publicRoutes.some((route) =>
68+
Boolean(
69+
matchPath(
70+
{ path: route.path, end: true },
71+
location.pathname,
72+
),
73+
),
74+
);
75+
6676
useEffect(() => {
77+
if (isPublicRoute) return;
6778
ensureAuth();
68-
}, [ensureAuth]);
79+
}, [ensureAuth, isPublicRoute]);
6980

7081
const isAuthBootstrapPending =
71-
!hydrated ||
72-
isInitializing ||
73-
(!isAuthenticated && refreshEnabled && !hasChecked);
82+
!isPublicRoute && (
83+
!hydrated ||
84+
isInitializing ||
85+
(!isAuthenticated && refreshEnabled && !hasChecked)
86+
);
7487

7588
if (isAuthBootstrapPending) {
7689
return (

0 commit comments

Comments
 (0)