Files
botsmann/lib/api-utils.ts
T
Mao NakamotoandClaude Opus 4.8 354b432326 chore(deps): upgrade Next.js 14 → 15.5.21 (clears 15 Dependabot alerts)
Bumps next + eslint-config-next 14.2.35 → 15.5.21. React stays on 18
(Next 15 supports React ^18.2.0), so no forced React 19 migration.

Clears all 15 open next-related Dependabot alerts (4 high) — the highest
patched version required is 15.5.16, so jumping to Next 16 would add
breakage (React 19, Turbopack default) for zero additional security gain.

Next 15 breaking-change migration:
- Async request APIs: params/searchParams awaited in the 6 dynamic pages
  that consume them as props; cookies() is now async, so
  createRouteHandlerClient() became async (9 call sites awaited).
- next.config: serverComponentsExternalPackages → top-level
  serverExternalPackages (renamed/destabilized in Next 15).
- typedRoutes tightening: object href {pathname, hash} is no longer
  accepted → string href "/path#hash" (4 governance pages).
- eslint: ignore the Next-generated next-env.d.ts (Next 15 adds a
  triple-slash routes.d.ts reference that trips no-triple-slash-reference).

Verified: `next build` green — compiles, type-checks, lints, and
prerenders all 81 pages. Client components using the useParams()
hook are unaffected and left untouched.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-24 00:25:12 +02:00

39 lines
1.3 KiB
TypeScript

/**
* API Authentication Utilities
* @module lib/api-utils
*
* Auth helpers for API routes. For response formatting, use lib/api/responses.
*/
import { NextRequest } from 'next/server';
import { getServiceClient } from './supabase';
import type { User } from '@supabase/supabase-js';
import { cookies } from 'next/headers';
import { createRouteHandlerClient } from './supabase-server';
/**
* Verify user from JWT token in Authorization header
* Extracts Bearer token and validates against Supabase
*
* @param request - Next.js request object
* @returns User object if valid, null otherwise
*/
export async function verifyUser(request: NextRequest): Promise<User | null> {
// Preferred: cookie-based via auth-helpers
try {
const routeClient = await createRouteHandlerClient({ cookies });
const { data, error } = await routeClient.auth.getUser();
if (!error && data.user) return data.user;
} catch {}
// Fallback: Bearer token header
const authHeader = request.headers.get('authorization');
if (authHeader?.startsWith('Bearer ')) {
const token = authHeader.substring(7);
const supabase = getServiceClient();
const { data, error } = await supabase.auth.getUser(token);
if (!error && data.user) return data.user;
}
return null;
}