1
- \callback.html
2
1
<!DOCTYPE html>
3
2
< html lang ="en ">
4
3
5
4
< head >
6
5
< meta charset ="UTF-8 ">
7
6
< 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 >
20
8
< script >
21
9
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
23
12
const redirectUri = 'https://itzsudipta.github.io/GrooveGraph/callback.html' ;
24
13
25
14
try {
26
15
const tokenResponse = await fetch ( 'https://accounts.spotify.com/api/token' , {
27
16
method : 'POST' ,
28
17
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
30
20
} ,
31
21
body : new URLSearchParams ( {
32
22
grant_type : 'authorization_code' ,
43
33
44
34
const tokens = await tokenResponse . json ( ) ;
45
35
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
47
37
} catch ( error ) {
48
38
console . error ( 'Token exchange error:' , error ) ;
49
39
document . getElementById ( 'error-message' ) . textContent = error . message ;
50
40
document . getElementById ( 'error-message' ) . classList . remove ( 'hidden' ) ;
51
41
}
52
42
}
53
43
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
+ } ) ;
63
65
</ script >
66
+ </ head >
67
+
68
+ < body >
69
+ < h1 > Redirecting...</ h1 >
70
+ < div id ="error-message " class ="hidden " style ="color: red; "> </ div >
64
71
</ body >
65
72
66
73
</ html >
0 commit comments