Files
daniel f5f7146888 feat: Add collections API endpoints and improve dashboard proxy
- Add Vercel serverless functions for user collections CRUD (collections.js, [id].js, items.js)
- Add shared auth (Clerk JWT verification) and Neon DB client helpers
- Add @clerk/backend dependency for token verification
- Improve dashboard API proxy: handle double-compression, error handling, filter accept-encoding
- Add retry logic to collections-api.ts for GET requests

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-22 16:32:23 -05:00

27 lines
787 B
JavaScript

import { verifyToken } from '@clerk/backend';
/**
* Extract and verify Clerk JWT from Authorization header.
* Returns the userId on success, or sends an error response and returns null.
*/
export async function authenticateRequest(req, res) {
const authHeader = req.headers.authorization;
if (!authHeader?.startsWith('Bearer ')) {
res.status(401).json({ error: 'Missing or invalid Authorization header' });
return null;
}
const token = authHeader.slice(7);
try {
const payload = await verifyToken(token, {
secretKey: process.env.CLERK_SECRET_KEY,
});
return payload.sub;
} catch (err) {
console.error('Clerk token verification failed:', err.message);
res.status(401).json({ error: 'Invalid or expired token' });
return null;
}
}