import type { APIRoute } from 'astro';
import { getTokenFromRequest, verifyToken } from '../../../../lib/auth';

export const GET: APIRoute = async ({ request }) => {
  const token = getTokenFromRequest(request);
  if (!token) return new Response(JSON.stringify({ authenticated: false }), { status: 401 });

  const payload = verifyToken(token);
  if (!payload) return new Response(JSON.stringify({ authenticated: false }), { status: 401 });

  return new Response(JSON.stringify({ authenticated: true, user: payload }), {
    headers: { 'Content-Type': 'application/json' },
  });
};
