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,7 +12,6 @@ import {
import { getDecisionWithOverrides } from "@/lib/decision-engine";
import { logger } from "@/lib/logger";
import { getNutritionGuidance } from "@/lib/nutrition";
import { createPocketBaseClient } from "@/lib/pocketbase";
import type { DailyData, DailyLog, HrvStatus } from "@/types";
// Default biometrics when no Garmin data is available
@@ -28,7 +27,7 @@ const DEFAULT_BIOMETRICS: {
weekIntensityMinutes: 0,
};
export const GET = withAuth(async (_request, user) => {
export const GET = withAuth(async (_request, user, pb) => {
// Validate required user data
if (!user.lastPeriodDate) {
return NextResponse.json(
@@ -70,7 +69,6 @@ export const GET = withAuth(async (_request, user) => {
// Try to fetch today's DailyLog for biometrics
let biometrics = { ...DEFAULT_BIOMETRICS, phaseLimit };
try {
const pb = createPocketBaseClient();
const today = new Date().toISOString().split("T")[0];
const dailyLog = await pb
.collection("dailyLogs")