feat(web): return-to-destination on auth redirect; logout pending state (#48)

This commit is contained in:
2026-06-08 15:06:50 +02:00
parent ec6e90ef5b
commit af3f1a5367
4 changed files with 38 additions and 8 deletions
+9 -4
View File
@@ -1,16 +1,21 @@
import { screen, waitFor } from "@testing-library/react"; import { screen, waitFor } from "@testing-library/react";
import { http, HttpResponse } from "msw"; import { http, HttpResponse } from "msw";
import { expect, test } from "vitest"; import { expect, test } from "vitest";
import { Route, Routes } from "react-router-dom"; import { Route, Routes, useLocation } from "react-router-dom";
import { server } from "../test/server"; import { server } from "../test/server";
import { renderApp } from "../test/render"; import { renderApp } from "../test/render";
import { RequireAuth } from "./require-auth"; import { RequireAuth } from "./require-auth";
function LoginStub() {
const location = useLocation();
return <div>login page {location.search}</div>;
}
function tree() { function tree() {
return ( return (
<Routes> <Routes>
<Route path="/login" element={<div>login page</div>} /> <Route path="/login" element={<LoginStub />} />
<Route element={<RequireAuth />}> <Route element={<RequireAuth />}>
<Route path="/objects" element={<div>secret objects</div>} /> <Route path="/objects" element={<div>secret objects</div>} />
</Route> </Route>
@@ -23,8 +28,8 @@ test("renders children when authenticated", async () => {
expect(await screen.findByText("secret objects")).toBeInTheDocument(); expect(await screen.findByText("secret objects")).toBeInTheDocument();
}); });
test("redirects to /login when unauthenticated", async () => { test("redirects unauthenticated users to /login carrying the attempted path", async () => {
server.use(http.get("/api/admin/me", () => new HttpResponse(null, { status: 401 }))); server.use(http.get("/api/admin/me", () => new HttpResponse(null, { status: 401 })));
renderApp(tree(), { route: "/objects" }); renderApp(tree(), { route: "/objects" });
await waitFor(() => expect(screen.getByText("login page")).toBeInTheDocument()); await waitFor(() => expect(screen.getByText(/from=%2Fobjects/)).toBeInTheDocument());
}); });
+6 -2
View File
@@ -1,14 +1,18 @@
import { Navigate, Outlet } from "react-router-dom"; import { Navigate, Outlet, useLocation } from "react-router-dom";
import { useMe } from "../api/queries"; import { useMe } from "../api/queries";
import { AppShellSkeleton } from "@/components/ui/skeletons"; import { AppShellSkeleton } from "@/components/ui/skeletons";
export function RequireAuth() { export function RequireAuth() {
const { data: user, isLoading } = useMe(); const { data: user, isLoading } = useMe();
const location = useLocation();
if (isLoading) return <AppShellSkeleton />; if (isLoading) return <AppShellSkeleton />;
if (!user) return <Navigate to="/login" replace />; if (!user) {
const from = encodeURIComponent(location.pathname + location.search);
return <Navigate to={`/login?from=${from}`} replace />;
}
return <Outlet />; return <Outlet />;
} }
+20 -1
View File
@@ -1,7 +1,7 @@
import { expect, test } from "vitest"; import { expect, test } from "vitest";
import { screen, waitFor, within } from "@testing-library/react"; import { screen, waitFor, within } from "@testing-library/react";
import userEvent from "@testing-library/user-event"; import userEvent from "@testing-library/user-event";
import { http, HttpResponse } from "msw"; import { delay, http, HttpResponse } from "msw";
import { server } from "../test/server"; import { server } from "../test/server";
import { renderApp } from "../test/render"; import { renderApp } from "../test/render";
import { UserMenu } from "./user-menu"; import { UserMenu } from "./user-menu";
@@ -33,3 +33,22 @@ test("opens the menu showing email + role and signs out", async () => {
await userEvent.click(signOut); await userEvent.click(signOut);
await waitFor(() => expect(loggedOut).toBe(true)); await waitFor(() => expect(loggedOut).toBe(true));
}); });
test("shows a pending state on Sign out while logging out", async () => {
server.use(
http.post("/api/admin/logout", async () => {
await delay(50);
return new HttpResponse(null, { status: 204 });
}),
);
renderApp(<UserMenu />);
const trigger = await screen.findByRole("button", { name: /editor@example.com/ });
await userEvent.click(trigger);
const menu = within(document.body);
await userEvent.click(await menu.findByText("Sign out"));
expect(await menu.findByText(/signing out/i)).toBeInTheDocument();
});
+3 -1
View File
@@ -35,7 +35,9 @@ export function UserMenu() {
<div className="text-xs text-muted-foreground">{me.role}</div> <div className="text-xs text-muted-foreground">{me.role}</div>
</div> </div>
<MenuSeparator /> <MenuSeparator />
<MenuItem onClick={onSignOut}>{t("auth.signOut")}</MenuItem> <MenuItem closeOnClick={false} disabled={logout.isPending} onClick={onSignOut}>
{logout.isPending ? t("auth.signingOut") : t("auth.signOut")}
</MenuItem>
</MenuContent> </MenuContent>
</Menu> </Menu>
); );