// Spotify OAuth logic const client_id = '5ed07e2a23384be495efff4d463aba54'; const redirect_uri = window.location.origin + window.location.pathname; const scope = 'user-read-private user-read-email user-read-playback-state user-modify-playback-state user-read-currently-playing'; function loginWithSpotify() { const code_verifier = generateCodeVerifier(); localStorage.setItem('code_verifier', code_verifier); generateCodeChallenge(code_verifier).then(code_challenge => { const params = new URLSearchParams({ response_type: 'code', client_id, scope, redirect_uri, code_challenge_method: 'S256', code_challenge }); window.location = 'https://accounts.spotify.com/authorize?' + params.toString(); }); } async function handleRedirect() { const params = new URLSearchParams(window.location.search); const code = params.get('code'); if (!code) return null; const code_verifier = localStorage.getItem('code_verifier'); const body = new URLSearchParams({ client_id, grant_type: 'authorization_code', code, redirect_uri, code_verifier }); const response = await fetch('https://accounts.spotify.com/api/token', { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, body }); const data = await response.json(); if (data.access_token) { window.history.replaceState({}, document.title, window.location.pathname); localStorage.removeItem('code_verifier'); localStorage.setItem('access_token', data.access_token); return data.access_token; } else { showError('Error: ' + JSON.stringify(data, null, 2)); return null; } }