-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
61 lines (51 loc) · 1.33 KB
/
index.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import 'dotenv/config'
import express from 'express'
import cors from 'cors'
import {randomUUID} from 'crypto'
import {
getToken,
provideToken,
refresh,
updateToken
} from './get_token'
import open from 'open'
const PORT = '7000' || process.env.PORT
function main() {
const app = express()
app.use(cors())
app.get('/', async (_, res) => {
const state = randomUUID()
const scope = 'user-read-recently-played'
const query = new URLSearchParams({
response_type: 'code',
client_id: process.env.CLIENT_ID,
scope: scope,
redirect_uri: 'http://localhost:' + PORT + '/callback',
state: state
})
res.redirect(
'https://accounts.spotify.com/authorize?' + query.toString()
)
})
app.get('/callback', async (req, res) => {
const code = req.query.code as string
const {access_token, expires_in, refresh_token} = await getToken(
code,
PORT
).then((res) => res.json())
res.send(access_token)
updateToken(access_token)
setTimeout(() => refresh(refresh_token), expires_in * 1000)
})
app.get('/token', async (_, res) => {
const token = provideToken()
if (token === '') {
open('http://localhost:' + PORT + '/')
}
res.send(provideToken())
})
app.listen(PORT, () => {
console.log('App listening on ', PORT)
})
}
main()