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

@@ -5,7 +5,6 @@ import { NextResponse } from "next/server";
import { withAuth } from "@/lib/auth-middleware";
import { logger } from "@/lib/logger";
import { createPocketBaseClient } from "@/lib/pocketbase";
import type { OverrideType } from "@/types";
const VALID_OVERRIDE_TYPES: OverrideType[] = [
@@ -27,7 +26,7 @@ function isValidOverrideType(value: unknown): value is OverrideType {
* Request body: { override: OverrideType }
* Response: { activeOverrides: OverrideType[] }
*/
export const POST = withAuth(async (request: NextRequest, user) => {
export const POST = withAuth(async (request: NextRequest, user, pb) => {
const body = await request.json();
if (!body.override) {
@@ -55,7 +54,6 @@ export const POST = withAuth(async (request: NextRequest, user) => {
: [...currentOverrides, overrideToAdd];
// Update the user record in PocketBase
const pb = createPocketBaseClient();
await pb
.collection("users")
.update(user.id, { activeOverrides: newOverrides });
@@ -74,7 +72,7 @@ export const POST = withAuth(async (request: NextRequest, user) => {
* Request body: { override: OverrideType }
* Response: { activeOverrides: OverrideType[] }
*/
export const DELETE = withAuth(async (request: NextRequest, user) => {
export const DELETE = withAuth(async (request: NextRequest, user, pb) => {
const body = await request.json();
if (!body.override) {
@@ -100,7 +98,6 @@ export const DELETE = withAuth(async (request: NextRequest, user) => {
const newOverrides = currentOverrides.filter((o) => o !== overrideToRemove);
// Update the user record in PocketBase
const pb = createPocketBaseClient();
await pb
.collection("users")
.update(user.id, { activeOverrides: newOverrides });