-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.ts
41 lines (37 loc) · 1.04 KB
/
server.ts
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
import express from 'express';
import * as bodyParser from 'body-parser';
import Spotify from './spotify';
export default class Server {
constructor(spotify: Spotify) {
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.get('/', async (req, res) => {
if (spotify.isAuthenticated) {
await spotify.refreshTokens();
res.sendStatus(200);
}
res.redirect(spotify.authorizationUrl);
});
app.get('/callback', async (req, res) => {
if (spotify.isAuthenticated) {
await spotify.refreshTokens();
res.sendStatus(200);
}
try {
await spotify.login(req.query.code.toString());
res.sendStatus(200);
} catch (e) {
res.sendStatus(404);
}
});
app.set('port', process.env.SERVER_PORT || 9000);
app.listen(app.get('port'), () => {
console.log(
`✅ Spotify auth server is live at http://localhost:${app.get(
'port',
)}, go log in!`,
);
});
}
}