-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathspotify-validator.js
More file actions
69 lines (46 loc) · 1.45 KB
/
Copy pathspotify-validator.js
File metadata and controls
69 lines (46 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/**
* Spotify Credentials Validator
* Validates Spotify Client ID and Secret by attempting authentication
*/
/**
* Validate Spotify credentials
* @param {string} clientId - Spotify Client ID
* @param {string} clientSecret - Spotify Client Secret
* @returns {Promise<{valid: boolean, error?: string}>}
*/
async function validateSpotifyCredentials(clientId, clientSecret) {
if (!clientId || clientId.trim() === '') {
return { valid: false, error: 'Client ID is required' };
}
if (!clientSecret || clientSecret.trim() === '') {
return { valid: false, error: 'Client Secret is required' };
}
try {
// Attempt to get an access token using client credentials flow
const response = await fetch('https://accounts.spotify.com/api/token', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'Basic ' + Buffer.from(`${clientId}:${clientSecret}`).toString('base64')
},
body: 'grant_type=client_credentials'
});
const data = await response.json();
if (response.ok && data.access_token) {
return { valid: true };
} else {
return {
valid: false,
error: data.error_description || data.error || 'Invalid credentials'
};
}
} catch (err) {
return {
valid: false,
error: err.message || 'Failed to connect to Spotify API'
};
}
}
module.exports = {
validateSpotifyCredentials
};