import { makeCookie, SPOTIFY_REDIRECT_URI } from "../helpers/spotifyServer"; export async function handle() { try { const clientId = process.env.SPOTIFY_CLIENT_ID; if (!clientId) { return new Response("Spotify client ID is not configured.", { status: 500 }); } const state = crypto.randomUUID().replace(/-/g, ""); const scopes = [ "playlist-read-private", "playlist-read-collaborative", "streaming", "user-read-email", "user-read-private", "user-read-playback-state", "user-modify-playback-state", "user-library-read", "user-follow-read", ].join(" "); const params = new URLSearchParams({ client_id: clientId, response_type: "code", redirect_uri: SPOTIFY_REDIRECT_URI, scope: scopes, state, show_dialog: "false", }); const headers = new Headers({ Location: `https://accounts.spotify.com/authorize?${params.toString()}`, "Cache-Control": "no-store", }); headers.append("Set-Cookie", makeCookie("spotirip_oauth_state", state, { maxAge: 600 })); return new Response("Redirecting to Spotify…", { status: 302, headers }); } catch (error) { const message = error instanceof Error ? error.message : "Unable to start Spotify login."; return new Response(message, { status: 500 }); } }