generated from antfu/starter-ts
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add example with authentication utils in playground
- Loading branch information
Showing
5 changed files
with
92 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,7 @@ | ||
# Issue a token with the required scopes in order to test your functions | ||
SPOTIFY_TOKEN= | ||
# Issue a token with the required scopes in order to test your functions without having to run the OAuth flow locally | ||
SPOTIFY_TOKEN= | ||
|
||
# Available from your dashboard https://developer.spotify.com/dashboard | ||
SPOTIFY_CLIENT_ID= | ||
SPOTIFY_CLIENT_SECRET= | ||
SPOTIFY_REDIRECT_URI= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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() | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.