Skip to content

Commit 9294275

Browse files
committed
fix(auth): Implement Spotify OAuth flow and data visualization
1 parent 0dde326 commit 9294275

File tree

1 file changed

+23
-22
lines changed

1 file changed

+23
-22
lines changed

script.js

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
const SPOTIFY_CONFIG = {
22
clientId: 'c3c6f141c28441f9bdd0988863be0d92',
3-
clientSecret: process.env.SPOTIFY_CLIENT_SECRET || '', // Add your client secret in .env
43
redirectUri: 'https://itzsudipta.github.io/GrooveGraph/callback.html',
5-
homePageUrl: 'https://itzsudipta.github.io/GrooveGraph/',
64
authEndpoint: 'https://accounts.spotify.com/authorize',
7-
tokenEndpoint: 'https://accounts.spotify.com/api/token',
85
apiEndpoint: 'https://api.spotify.com/v1',
96
scopes: [
107
'user-top-read',
@@ -19,24 +16,17 @@ const SPOTIFY_CONFIG = {
1916
class SpotifyAuth {
2017
constructor(config) {
2118
this.config = config;
19+
this.initializeLoginButton();
2220
this.checkAuthenticationOnLoad();
2321
}
2422

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());
3327
}
3428
}
3529

36-
generateState() {
37-
return crypto.randomUUID();
38-
}
39-
4030
async initiateLogin() {
4131
try {
4232
const state = this.generateState();
@@ -51,12 +41,28 @@ class SpotifyAuth {
5141
show_dialog: true
5242
});
5343

54-
window.location.href = `${this.config.authEndpoint}?${params}`;
44+
const authUrl = `${this.config.authEndpoint}?${params.toString()}`;
45+
window.location.href = authUrl;
5546
} catch (error) {
5647
this.handleError('Failed to initialize login');
5748
}
5849
}
5950

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+
6066
async exchangeCodeForToken(code) {
6167
try {
6268
const response = await fetch(this.config.tokenEndpoint, {
@@ -97,7 +103,7 @@ class SpotifyAuth {
97103
const errorDiv = document.getElementById('error-message');
98104
if (errorDiv) {
99105
errorDiv.textContent = message;
100-
errorDiv.style.display = 'block';
106+
errorDiv.classList.remove('hidden');
101107
}
102108
}
103109
}
@@ -182,11 +188,6 @@ const dataVisualizer = new DataVisualizer();
182188

183189
// Event Listeners
184190
document.addEventListener('DOMContentLoaded', async () => {
185-
const loginButton = document.getElementById('loginButton');
186-
if (loginButton) {
187-
loginButton.addEventListener('click', () => spotifyAuth.initiateLogin());
188-
}
189-
190191
if (sessionStorage.getItem('spotify_access_token')) {
191192
try {
192193
dataVisualizer.showLoading();

0 commit comments

Comments
 (0)