generated from antfu/starter-ts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.ts
72 lines (59 loc) · 2 KB
/
auth.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
62
63
64
65
66
67
68
69
70
71
72
import { createServer } from 'node:http'
import { parse } from 'node:url'
import { consola } from 'consola'
import { Scopes, SpotifyClient } from 'spotify-web-client'
console.clear()
const client = new SpotifyClient()
// Test your functions here
const link = client.generateOAuthUrl({
clientId: import.meta.env.SPOTIFY_CLIENT_ID!,
redirectUri: import.meta.env.SPOTIFY_REDIRECT_URI!,
scope: [
Scopes.USER_READ_PRIVATE,
Scopes.USER_READ_EMAIL,
],
})
consola.box('Spotify OAuth2 Playground')
consola.info('Open the following link to authenticate:')
console.log(link)
const server = createServer(async (req, res) => {
const url = parse(req.url!, true)
if (url.pathname === '/api/auth/callback') {
const code = url.query.code as string
const response = await client.getAccessToken({
code,
redirect_uri: import.meta.env.SPOTIFY_REDIRECT_URI!,
client_secret: import.meta.env.SPOTIFY_CLIENT_SECRET!,
client_id: import.meta.env.SPOTIFY_CLIENT_ID!,
})
consola.success('Successfully authenticated')
consola.info('Access token:', response.access_token)
console.debug('Refreshing token ...')
const refreshed = await client.refreshToken({
client_id: import.meta.env.SPOTIFY_CLIENT_ID!,
client_secret: import.meta.env.SPOTIFY_CLIENT_SECRET!,
})
consola.success('Successfully refreshed token')
consola.success('Access token:', refreshed.access_token)
// Testing our token
const profile = await client
.setAccessToken(response.access_token, response.expires_in)
.users
.getCurrentUserProfile()
consola.info('User email:', profile.email)
consola.info('User display name:', profile.display_name)
// Send the response to the client
res.writeHead(200, { 'Content-Type': 'application/json' })
res.write(JSON.stringify(response))
res.end()
}
})
if (import.meta.hot) {
import.meta.hot.on('vite:beforeFullReload', () => {
server.close()
})
}
server.listen(3000)
process.on('exit', () => {
server.close()
})