Skip to content

Commit 0220a55

Browse files
committed
Add proper state validation
1 parent 281eb05 commit 0220a55

File tree

1 file changed

+32
-25
lines changed

1 file changed

+32
-25
lines changed

callback.html

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,22 @@
1-
\callback.html
21
<!DOCTYPE html>
32
<html lang="en">
43

54
<head>
65
<meta charset="UTF-8">
76
<meta name="viewport" content="width=device-width, initial-scale=1.0">
8-
<meta http-equiv="X-Content-Type-Options" content="nosniff">
9-
<title>Authentication - GrooveGraph</title>
10-
<link rel="stylesheet" href="style.css">
11-
</head>
12-
13-
<body>
14-
<div class="loading-container">
15-
<div class="spinner"></div>
16-
<p>Completing authentication...</p>
17-
<div id="error-message" class="error-message hidden"></div>
18-
</div>
19-
7+
<title>Callback</title>
208
<script>
219
async function exchangeCodeForToken(code) {
22-
const clientId = 'c3c6f141c28441f9bdd0988863be0d92';
10+
const clientId = 'c3c6f141c28441f9bdd0988863be0d92'; // Replace with your actual client ID
11+
const clientSecret = 'ff1254589c8846248f3b705c3a3c2251'; // Replace with your actual client secret
2312
const redirectUri = 'https://itzsudipta.github.io/GrooveGraph/callback.html';
2413

2514
try {
2615
const tokenResponse = await fetch('https://accounts.spotify.com/api/token', {
2716
method: 'POST',
2817
headers: {
29-
'Content-Type': 'application/x-www-form-urlencoded'
18+
'Content-Type': 'application/x-www-form-urlencoded',
19+
'Authorization': 'Basic ' + btoa(clientId + ':' + clientSecret) // Base64 encoded client ID and secret
3020
},
3121
body: new URLSearchParams({
3222
grant_type: 'authorization_code',
@@ -43,24 +33,41 @@
4333

4434
const tokens = await tokenResponse.json();
4535
sessionStorage.setItem('spotify_access_token', tokens.access_token);
46-
window.location.href = 'https://itzsudipta.github.io/GrooveGraph/';
36+
window.location.href = 'https://itzsudipta.github.io/GrooveGraph/'; // Redirect to your main page
4737
} catch (error) {
4838
console.error('Token exchange error:', error);
4939
document.getElementById('error-message').textContent = error.message;
5040
document.getElementById('error-message').classList.remove('hidden');
5141
}
5242
}
5343

54-
// Handle the callback
55-
const urlParams = new URLSearchParams(window.location.search);
56-
const code = urlParams.get('code');
57-
if (code) {
58-
exchangeCodeForToken(code);
59-
} else {
60-
document.getElementById('error-message').textContent = 'Authentication failed: No code received';
61-
document.getElementById('error-message').classList.remove('hidden');
62-
}
44+
document.addEventListener('DOMContentLoaded', () => {
45+
const params = new URLSearchParams(window.location.search); // Changed to use query string params
46+
const code = params.get('code');
47+
const error = params.get('error');
48+
49+
if (error) {
50+
console.error('Error during authentication:', error);
51+
document.getElementById('error-message').textContent = 'Authentication failed. Please try again.';
52+
document.getElementById('error-message').classList.remove('hidden');
53+
return;
54+
}
55+
56+
if (code) {
57+
// Call the function to exchange the code for a token
58+
exchangeCodeForToken(code);
59+
} else {
60+
console.error('No code found');
61+
document.getElementById('error-message').textContent = 'Authentication failed. Please try again.';
62+
document.getElementById('error-message').classList.remove('hidden');
63+
}
64+
});
6365
</script>
66+
</head>
67+
68+
<body>
69+
<h1>Redirecting...</h1>
70+
<div id="error-message" class="hidden" style="color: red;"></div>
6471
</body>
6572

6673
</html>

0 commit comments

Comments
 (0)