Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 6 additions & 12 deletions apps/web/src/components/app-shell/user-menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@ import {
SidebarMenuButton,
SidebarMenuItem,
} from "@/components/ui/sidebar";
import {
currentUserOptionalQueryOptions,
currentUserQueryOptions,
logout,
} from "@/lib/auth-api";
import { currentUserOptionalQueryOptions, logout } from "@/lib/auth-api";

function getInitials(name: string): string {
return name
Expand Down Expand Up @@ -66,13 +62,11 @@ export function UserMenu() {
setIsLoggingOut(true);
try {
await logout();
await queryClient.cancelQueries({
queryKey: currentUserQueryOptions.queryKey,
});
queryClient.removeQueries({
queryKey: currentUserQueryOptions.queryKey,
exact: true,
});
// Clear ALL React Query caches so no stale data from the just-logged-out
// user (e.g. "auth"/"me-optional" used by the sidebar, permissions,
// projects, etc.) leaks into the next session. The login route will
// re-fetch everything from scratch.
queryClient.clear();
await navigate({ to: "/", replace: true });
} finally {
setIsLoggingOut(false);
Expand Down
6 changes: 4 additions & 2 deletions apps/web/src/hooks/use-login-form.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { act, renderHook } from "@testing-library/react";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { ApiErrorCode } from "@/lib/api-error";
import { currentUserQueryOptions, login } from "@/lib/auth-api";
import { login } from "@/lib/auth-api";
import { useLoginForm } from "./use-login-form";

type SubmitPayload = {
Expand Down Expand Up @@ -91,8 +91,10 @@ describe("useLoginForm", () => {
});

expect(login).toHaveBeenCalledWith("alice", "password123", true);
// The login flow invalidates the entire "auth" namespace so both the
// "auth"/"me" and "auth"/"me-optional" caches are refreshed.
expect(mocks.invalidateQueriesMock).toHaveBeenCalledWith({
queryKey: currentUserQueryOptions.queryKey,
queryKey: ["auth"],
});
expect(mocks.navigateMock).toHaveBeenCalledWith({ to: "/home" });
});
Expand Down
9 changes: 5 additions & 4 deletions apps/web/src/hooks/use-login-form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useQueryClient } from "@tanstack/react-query";
import { useNavigate } from "@tanstack/react-router";
import { useState } from "react";
import { ApiErrorCode, getApiErrorCode } from "@/lib/api-error";
import { currentUserQueryOptions, login } from "@/lib/auth-api";
import { login } from "@/lib/auth-api";

const loginErrorMessages: Partial<Record<ApiErrorCode, string>> = {
[ApiErrorCode.InvalidCredentials]: "Invalid username or password.",
Expand All @@ -25,9 +25,10 @@ export function useLoginForm() {
setServerError(null);
try {
await login(value.username, value.password, value.rememberMe);
await queryClient.invalidateQueries({
queryKey: currentUserQueryOptions.queryKey,
});
// Invalidate the entire "auth" query namespace so both the required
// ("auth"/"me") and the optional ("auth"/"me-optional") caches are
// refreshed. Without this the sidebar keeps the previous user's data.
await queryClient.invalidateQueries({ queryKey: ["auth"] });
await navigate({ to: "/home" });
} catch (err: unknown) {
const code = getApiErrorCode(err);
Expand Down
Loading