import { verifyToken, getUserById } from '../auth-utils.js'; export default async function handler(req, res) { // Set CORS headers res.setHeader('Access-Control-Allow-Origin', '*'); res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS'); res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization'); // Handle preflight requests if (req.method === 'OPTIONS') { res.status(200).end(); return; } if (req.method !== 'GET') { return res.status(405).json({ error: 'Method not allowed' }); } try { const authHeader = req.headers.authorization; if (!authHeader || !authHeader.startsWith('Bearer ')) { return res.status(401).json({ error: 'No token provided' }); } const token = authHeader.substring(7); const decoded = verifyToken(token); if (!decoded) { return res.status(401).json({ error: 'Invalid token' }); } // Get user data const user = await getUserById(decoded.userId); if (!user) { return res.status(401).json({ error: 'User not found' }); } res.status(200).json({ success: true, user: { id: user.id, email: user.email, role: user.role, created_at: user.created_at } }); } catch (error) { console.error('Token verification error:', error); res.status(500).json({ error: 'Internal server error' }); } }