import type { APIRoute } from 'astro'; import { generateAuthUrl, generateState, logAuthEvent } from '../../../utils/auth.js'; export const prerender = false; export const GET: APIRoute = async ({ url, redirect }) => { try { const state = generateState(); const authUrl = generateAuthUrl(state); // Debug: log the generated URL console.log('Generated auth URL:', authUrl); // Get the intended destination after login (if any) const returnTo = url.searchParams.get('returnTo') || '/'; logAuthEvent('Login initiated', { returnTo, authUrl }); // Store state and returnTo in a cookie for the callback const stateData = JSON.stringify({ state, returnTo }); const stateCookie = `auth_state=${encodeURIComponent(stateData)}; HttpOnly; SameSite=Lax; Path=/; Max-Age=600`; // 10 minutes return new Response(null, { status: 302, headers: { 'Location': authUrl, 'Set-Cookie': stateCookie } }); } catch (error) { logAuthEvent('Login failed', { error: error.message }); return new Response('Authentication error', { status: 500 }); } };