Files
Perplexica/src/middleware/auth.ts
eligrinfeld 9f4ae1baac feat: update backend services and routes
- Add business routes and middleware\n- Update search and database services\n- Improve health check implementation\n- Update CI workflow configuration
2025-01-06 21:25:15 -07:00

47 lines
1.1 KiB
TypeScript

import { Request, Response, NextFunction } from 'express';
import { supabase } from '../lib/supabase';
// Extend Express Request type to include user
declare global {
namespace Express {
interface Request {
user?: {
id: string;
email: string;
role: string;
};
}
}
}
export async function authenticateUser(
req: Request,
res: Response,
next: NextFunction
) {
try {
const authHeader = req.headers.authorization;
if (!authHeader) {
return res.status(401).json({ error: 'No authorization header' });
}
const token = authHeader.replace('Bearer ', '');
const { data: { user }, error } = await supabase.auth.getUser(token);
if (error || !user) {
return res.status(401).json({ error: 'Invalid token' });
}
// Add user info to request
req.user = {
id: user.id,
email: user.email!,
role: (user.app_metadata?.role as string) || 'user'
};
next();
} catch (error) {
console.error('Authentication error:', error);
res.status(401).json({ error: 'Authentication failed' });
}
}