mirror of
https://github.com/ItzCrazyKns/Perplexica.git
synced 2025-09-17 06:41:33 +00:00
- Add business routes and middleware\n- Update search and database services\n- Improve health check implementation\n- Update CI workflow configuration
47 lines
1.1 KiB
TypeScript
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' });
|
|
}
|
|
}
|