1
1
const SPOTIFY_CONFIG = {
2
2
clientId : 'c3c6f141c28441f9bdd0988863be0d92' ,
3
- clientSecret : process . env . SPOTIFY_CLIENT_SECRET || '' , // Add your client secret in .env
4
3
redirectUri : 'https://itzsudipta.github.io/GrooveGraph/callback.html' ,
5
- homePageUrl : 'https://itzsudipta.github.io/GrooveGraph/' ,
6
4
authEndpoint : 'https://accounts.spotify.com/authorize' ,
7
- tokenEndpoint : 'https://accounts.spotify.com/api/token' ,
8
5
apiEndpoint : 'https://api.spotify.com/v1' ,
9
6
scopes : [
10
7
'user-top-read' ,
@@ -19,24 +16,17 @@ const SPOTIFY_CONFIG = {
19
16
class SpotifyAuth {
20
17
constructor ( config ) {
21
18
this . config = config ;
19
+ this . initializeLoginButton ( ) ;
22
20
this . checkAuthenticationOnLoad ( ) ;
23
21
}
24
22
25
- checkAuthenticationOnLoad ( ) {
26
- const params = new URLSearchParams ( window . location . search ) ;
27
- const code = params . get ( 'code' ) ;
28
- const state = params . get ( 'state' ) ;
29
- const storedState = sessionStorage . getItem ( 'spotify_auth_state' ) ;
30
-
31
- if ( code && state === storedState ) {
32
- this . exchangeCodeForToken ( code ) ;
23
+ initializeLoginButton ( ) {
24
+ const loginButton = document . getElementById ( 'loginButton' ) ;
25
+ if ( loginButton ) {
26
+ loginButton . addEventListener ( 'click' , ( ) => this . initiateLogin ( ) ) ;
33
27
}
34
28
}
35
29
36
- generateState ( ) {
37
- return crypto . randomUUID ( ) ;
38
- }
39
-
40
30
async initiateLogin ( ) {
41
31
try {
42
32
const state = this . generateState ( ) ;
@@ -51,12 +41,28 @@ class SpotifyAuth {
51
41
show_dialog : true
52
42
} ) ;
53
43
54
- window . location . href = `${ this . config . authEndpoint } ?${ params } ` ;
44
+ const authUrl = `${ this . config . authEndpoint } ?${ params . toString ( ) } ` ;
45
+ window . location . href = authUrl ;
55
46
} catch ( error ) {
56
47
this . handleError ( 'Failed to initialize login' ) ;
57
48
}
58
49
}
59
50
51
+ generateState ( ) {
52
+ return crypto . randomUUID ( ) ;
53
+ }
54
+
55
+ checkAuthenticationOnLoad ( ) {
56
+ const params = new URLSearchParams ( window . location . search ) ;
57
+ const code = params . get ( 'code' ) ;
58
+ const state = params . get ( 'state' ) ;
59
+ const storedState = sessionStorage . getItem ( 'spotify_auth_state' ) ;
60
+
61
+ if ( code && state === storedState ) {
62
+ this . exchangeCodeForToken ( code ) ;
63
+ }
64
+ }
65
+
60
66
async exchangeCodeForToken ( code ) {
61
67
try {
62
68
const response = await fetch ( this . config . tokenEndpoint , {
@@ -97,7 +103,7 @@ class SpotifyAuth {
97
103
const errorDiv = document . getElementById ( 'error-message' ) ;
98
104
if ( errorDiv ) {
99
105
errorDiv . textContent = message ;
100
- errorDiv . style . display = 'block' ;
106
+ errorDiv . classList . remove ( 'hidden' ) ;
101
107
}
102
108
}
103
109
}
@@ -182,11 +188,6 @@ const dataVisualizer = new DataVisualizer();
182
188
183
189
// Event Listeners
184
190
document . addEventListener ( 'DOMContentLoaded' , async ( ) => {
185
- const loginButton = document . getElementById ( 'loginButton' ) ;
186
- if ( loginButton ) {
187
- loginButton . addEventListener ( 'click' , ( ) => spotifyAuth . initiateLogin ( ) ) ;
188
- }
189
-
190
191
if ( sessionStorage . getItem ( 'spotify_access_token' ) ) {
191
192
try {
192
193
dataVisualizer . showLoading ( ) ;
0 commit comments