Files
botsmann/lib/api-utils.ts
T
g-butandClaude Opus 4.6 2d5dc85501 refactor: remove dead code, archive historical docs, fix README
- Remove duplicate ApiResponse, apiSuccess, apiError, withErrorHandling,
  requireAuth, ERROR_MESSAGES from lib/api-utils.ts — only verifyUser
  was actually imported anywhere. Response utils SSOT is lib/api/responses.ts
- Archive 14 historical LEX-era design docs to docs/archive/ — docs/
  now has only 9 active reference files (down from 29)
- Remove mock data and placeholder images from Amazon/Ricardo platform
  integrations — functions return [] until real APIs are implemented
- Update platform tests to match actual behavior
- Fix README.md: update backend tech (Groq/OpenRouter), add Supabase
  to prerequisites

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-10 10:46:04 +01: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/auth-helpers-nextjs';
/**
* 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 = 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;
}