import { Router } from 'express'; import { AuthManager } from '../auth/index.ts'; export default (auth: AuthManager) => { const router = Router(); router.get('/status', async (req, res) => { try { const sessionId = req.header('x-session-id'); if (!sessionId) { return res.status(400).json({ error: 'Missing x-session-id header', }); } const hasPending = auth.hasPendingDeviceFlow(sessionId); const userId = auth.getUserIdFromSession(sessionId); const flowError = auth.getPendingFlowError(sessionId); if (userId) { return res.json({ authenticated: true, pending: false, userId, }); } if (flowError) { const err = flowError as { errorCode?: string; errorMessage?: string; message?: string; }; const errorCode = err.errorCode ?? 'unknown_error'; const errorMessage = err.errorMessage ?? err.message ?? 'unknown'; return res.json({ authenticated: false, pending: false, error: errorCode, message: errorCode === 'device_code_expired' ? 'Device code has expired. Please login again.' : `Authentication failed: ${errorMessage}`, }); } if (hasPending) { return res.json({ authenticated: false, pending: true, message: 'Waiting for device code authentication', }); } return res.json({ authenticated: false, pending: false, message: 'Session not found or expired', }); } catch (err) { const error = err instanceof Error ? err.message : 'Unknown error'; return res.status(500).json({ error }); } }); return router; };