Fix 404 error when saving user preferences

Routes using withAuth were creating new unauthenticated PocketBase
clients, causing 404 errors when trying to update records. Modified
withAuth to pass the authenticated pb client to handlers so they can
use it for database operations.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-01-12 16:45:55 +00:00
parent df2f52ad50
commit 2408839b8b
17 changed files with 91 additions and 119 deletions

View File

@@ -12,24 +12,19 @@ let currentMockUser: User | null = null;
// Module-level variable to control mock daily log in tests
let currentMockDailyLog: DailyLog | null = null;
// Mock PocketBase client for database operations
vi.mock("@/lib/pocketbase", () => ({
createPocketBaseClient: vi.fn(() => ({
collection: vi.fn(() => ({
getFirstListItem: vi.fn(async () => {
if (!currentMockDailyLog) {
const error = new Error("No DailyLog found");
(error as { status?: number }).status = 404;
throw error;
}
return currentMockDailyLog;
}),
})),
// Create mock PocketBase client
const mockPb = {
collection: vi.fn(() => ({
getFirstListItem: vi.fn(async () => {
if (!currentMockDailyLog) {
const error = new Error("No DailyLog found");
(error as { status?: number }).status = 404;
throw error;
}
return currentMockDailyLog;
}),
})),
loadAuthFromCookies: vi.fn(),
isAuthenticated: vi.fn(() => currentMockUser !== null),
getCurrentUser: vi.fn(() => currentMockUser),
}));
};
// Mock the auth-middleware module
vi.mock("@/lib/auth-middleware", () => ({
@@ -38,7 +33,7 @@ vi.mock("@/lib/auth-middleware", () => ({
if (!currentMockUser) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}
return handler(request, currentMockUser);
return handler(request, currentMockUser, mockPb);
};
}),
}));