2025-07-24 17:36:10 -04:00
|
|
|
import { sql } from '@vercel/postgres';
|
|
|
|
|
import jwt from 'jsonwebtoken';
|
2026-05-23 10:47:11 -04:00
|
|
|
import { JWT_SECRET } from '../../../lib/auth-secret.js';
|
2025-07-23 22:26:54 -04:00
|
|
|
|
|
|
|
|
export default async function handler(req, res) {
|
|
|
|
|
if (req.method !== 'GET') {
|
|
|
|
|
return res.status(405).json({ error: 'Method not allowed' });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
2025-07-24 17:36:10 -04:00
|
|
|
const authHeader = req.headers.authorization;
|
|
|
|
|
|
|
|
|
|
if (!authHeader || !authHeader.startsWith('Bearer ')) {
|
2026-05-23 11:47:27 -04:00
|
|
|
return res.status(401).json({ error: 'Authentication required' });
|
2025-07-24 17:36:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const token = authHeader.substring(7);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const decoded = jwt.verify(token, JWT_SECRET);
|
|
|
|
|
|
|
|
|
|
// Get user data from database
|
|
|
|
|
const result = await sql`
|
|
|
|
|
SELECT id, email, role, created_at
|
|
|
|
|
FROM users
|
|
|
|
|
WHERE id = ${decoded.userId}
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
if (result.rows.length === 0) {
|
|
|
|
|
return res.status(401).json({ error: 'User not found' });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const user = result.rows[0];
|
|
|
|
|
res.status(200).json(user);
|
|
|
|
|
|
|
|
|
|
} catch (jwtError) {
|
|
|
|
|
console.error('JWT verification error:', jwtError);
|
|
|
|
|
return res.status(401).json({ error: 'Invalid token' });
|
|
|
|
|
}
|
2025-07-23 22:26:54 -04:00
|
|
|
|
|
|
|
|
} catch (error) {
|
2025-07-24 17:36:10 -04:00
|
|
|
console.error('Auth verification error:', error);
|
2025-07-23 22:26:54 -04:00
|
|
|
res.status(500).json({ error: 'Internal server error' });
|
|
|
|
|
}
|
|
|
|
|
}
|