Repository navigation
Expand file tree
/
Copy pathauth.js
More file actions
96 lines (80 loc) · 3.08 KB
/
Copy pathauth.js
File metadata and controls
96 lines (80 loc) · 3.08 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
require('dotenv').config({ quiet: true });
const express = require('express');
const { saveToken } = require('./spotify-token-store');
const app = express();
const CLIENT_ID = process.env.SPOTIFY_CLIENT_ID;
const CLIENT_SECRET = process.env.SPOTIFY_CLIENT_SECRET;
const REDIRECT_URI = process.env.SPOTIFY_REDIRECT_URI || 'http://127.0.0.1:8000/callback';
const redirectUrl = new URL(REDIRECT_URI);
const isLocalDevelopmentRedirect = redirectUrl.protocol === 'http:' && redirectUrl.hostname === '127.0.0.1';
if (redirectUrl.protocol !== 'https:' && !isLocalDevelopmentRedirect) {
console.error('SPOTIFY_REDIRECT_URI must use HTTPS, except for http://127.0.0.1 during local development.');
process.exit(1);
}
const PORT = redirectUrl.port || 8000;
const SCOPES = 'user-modify-playback-state user-read-currently-playing user-read-playback-state';
if (!CLIENT_ID || !CLIENT_SECRET) {
console.error('Missing SPOTIFY_CLIENT_ID or SPOTIFY_CLIENT_SECRET in .env');
process.exit(1);
}
app.get('/login', (req, res) => {
const params = new URLSearchParams({
client_id: CLIENT_ID,
response_type: 'code',
redirect_uri: REDIRECT_URI,
scope: SCOPES
});
res.redirect('https://accounts.spotify.com/authorize?' + params);
});
app.get('/callback', async (req, res) => {
const code = req.query.code;
const spotifyError = req.query.error;
if (spotifyError) {
res.status(400).type('text/plain').send(`Spotify authorization failed: ${spotifyError}`);
return;
}
if (!code) {
res.status(400).send('Missing Spotify authorization code');
return;
}
try {
const response = await fetch('https://accounts.spotify.com/api/token', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'Basic ' + Buffer.from(CLIENT_ID + ':' + CLIENT_SECRET).toString('base64')
},
body: new URLSearchParams({
grant_type: 'authorization_code',
code,
redirect_uri: REDIRECT_URI
})
});
const data = await response.json();
if (!response.ok) {
console.error('Spotify token exchange failed:', data);
res.status(response.status).send('Spotify token exchange failed. Check the terminal for details.');
return;
}
if (!data.access_token || !data.refresh_token || !data.expires_in) {
console.error('Spotify token response was missing expected fields:', data);
res.status(500).send('Spotify token response was missing expected fields.');
return;
}
saveToken({
access_token: data.access_token,
refresh_token: data.refresh_token,
expires_at: Date.now() + data.expires_in * 1000
});
res.send('Spotify tokens saved to spotify-token.json. You can close this tab owo');
server.close(() => {
console.log('Spotify auth complete.');
});
} catch (err) {
console.error('Spotify auth failed:', err);
res.status(500).send('Spotify auth failed. Check the terminal for details.');
}
});
const server = app.listen(PORT, () => {
console.log(`Open this in your browser: ${new URL('/login', REDIRECT_URI).toString()}`);
});