Skip to content

Commit

Permalink
feat: add example with authentication utils in playground
Browse files Browse the repository at this point in the history
  • Loading branch information
noook committed Jul 25, 2023
1 parent 57ce485 commit d52f704
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 10 deletions.
9 changes: 7 additions & 2 deletions playground/.env
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=
72 changes: 72 additions & 0 deletions playground/auth.ts
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()
})
4 changes: 2 additions & 2 deletions playground/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { SearchType, SpotifyClient } from '@noook/spotify-client'
import { SearchType, SpotifyClient } from 'spotify-web-client'

console.clear()
const client = new SpotifyClient().setAccessToken(import.meta.env.SPOTIFY_TOKEN!)
const client = new SpotifyClient().setAccessToken(import.meta.env.SPOTIFY_TOKEN!, 3600)

// Test your functions here
const searchResult = await client.search.search(
Expand Down
5 changes: 3 additions & 2 deletions playground/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@
"version": "0.0.0",
"private": true,
"scripts": {
"start": "vite-node -w index.ts"
"start": "vite-node -w"
},
"dependencies": {
"@noook/spotify-client": "workspace:*",
"spotify-web-client": "workspace:*",
"vite-node": "^0.33.0",
"vitest": "^0.32.2"
},
"devDependencies": {
"@types/node": "^18.14.1",
"consola": "^3.2.3",
"typescript": "5.1.3",
"vite": "^4.3.9"
}
Expand Down
12 changes: 8 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d52f704

Please sign in to comment.