103 lines
2.5 KiB
JavaScript
103 lines
2.5 KiB
JavaScript
import React from "react";
|
|
import { Navigate, createBrowserRouter, useParams } from "react-router-dom";
|
|
import App from "./App";
|
|
import { ClientDeliveryPage } from "./pages/ClientDeliveryPage";
|
|
import { ClientTestPage } from "./pages/ClientTestPage";
|
|
import { DashboardPage } from "./pages/DashboardPage";
|
|
import { GroupDetailPage } from "./pages/GroupDetailPage";
|
|
import { LoginPage } from "./pages/LoginPage";
|
|
import { NotFoundPage } from "./pages/NotFoundPage";
|
|
import { ForbiddenPage } from "./pages/ForbiddenPage";
|
|
import { SettingsPage } from "./pages/SettingsPage";
|
|
import { useAuth } from "./context/AuthContext";
|
|
|
|
/**
|
|
* Protects routes that require authentication.
|
|
* Redirects to /login with return URL if no user and session is loaded.
|
|
* Shows nothing while session is being restored (avoids flash-of-login).
|
|
*/
|
|
const RequireAuth = ({ children }) => {
|
|
const { user, isSessionLoading } = useAuth();
|
|
|
|
if (isSessionLoading) {
|
|
return null;
|
|
}
|
|
|
|
if (!user) {
|
|
const redirect = encodeURIComponent(window.location.pathname + window.location.search);
|
|
return <Navigate to={`/login?redirect=${redirect}`} replace />;
|
|
}
|
|
|
|
return children;
|
|
};
|
|
|
|
export const router = createBrowserRouter([
|
|
{
|
|
path: "/",
|
|
element: <App />,
|
|
children: [
|
|
{
|
|
index: true,
|
|
element: <Navigate to="/client" replace />,
|
|
},
|
|
{
|
|
path: "login",
|
|
element: <LoginPage />,
|
|
},
|
|
{
|
|
path: "client",
|
|
element: <ClientDeliveryPage />,
|
|
},
|
|
{
|
|
path: "client/:token",
|
|
element: <ClientDeliveryPage />,
|
|
},
|
|
{
|
|
path: "client/test",
|
|
element: <ClientTestPage />,
|
|
},
|
|
{
|
|
path: "delivery/:token",
|
|
element: <LegacyDeliveryRedirect />,
|
|
},
|
|
{
|
|
path: "forbidden",
|
|
element: <ForbiddenPage />,
|
|
},
|
|
{
|
|
path: "dashboard",
|
|
element: (
|
|
<RequireAuth>
|
|
<DashboardPage />
|
|
</RequireAuth>
|
|
),
|
|
},
|
|
{
|
|
path: "dashboard/group/:groupId",
|
|
element: (
|
|
<RequireAuth>
|
|
<GroupDetailPage />
|
|
</RequireAuth>
|
|
),
|
|
},
|
|
{
|
|
path: "settings",
|
|
element: (
|
|
<RequireAuth>
|
|
<SettingsPage />
|
|
</RequireAuth>
|
|
),
|
|
},
|
|
{
|
|
path: "*",
|
|
element: <NotFoundPage />,
|
|
},
|
|
],
|
|
},
|
|
]);
|
|
|
|
function LegacyDeliveryRedirect() {
|
|
const { token } = useParams();
|
|
return <Navigate to={`/client/${encodeURIComponent(token || "")}`} replace />;
|
|
}
|