From d52f7043e5b6bbe1b5e9ca52cb701f1d8091ded8 Mon Sep 17 00:00:00 2001 From: Neil Richter Date: Tue, 25 Jul 2023 15:32:03 +0200 Subject: [PATCH] feat: add example with authentication utils in playground --- playground/.env | 9 ++++-- playground/auth.ts | 72 +++++++++++++++++++++++++++++++++++++++++ playground/index.ts | 4 +-- playground/package.json | 5 +-- pnpm-lock.yaml | 12 ++++--- 5 files changed, 92 insertions(+), 10 deletions(-) create mode 100644 playground/auth.ts diff --git a/playground/.env b/playground/.env index 40c4b3c..c139a60 100644 --- a/playground/.env +++ b/playground/.env @@ -1,2 +1,7 @@ -# Issue a token with the required scopes in order to test your functions -SPOTIFY_TOKEN= \ No newline at end of file +# 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= \ No newline at end of file diff --git a/playground/auth.ts b/playground/auth.ts new file mode 100644 index 0000000..7fca024 --- /dev/null +++ b/playground/auth.ts @@ -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() +}) diff --git a/playground/index.ts b/playground/index.ts index 14f804e..d0472dc 100644 --- a/playground/index.ts +++ b/playground/index.ts @@ -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( diff --git a/playground/package.json b/playground/package.json index 5d28488..335b20a 100644 --- a/playground/package.json +++ b/playground/package.json @@ -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" } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 49dc105..e7d68f1 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -63,7 +63,7 @@ importers: playground: dependencies: - '@noook/spotify-client': + spotify-web-client: specifier: workspace:* version: link:.. vite-node: @@ -76,6 +76,9 @@ importers: '@types/node': specifier: ^18.14.1 version: 18.16.16 + consola: + specifier: ^3.2.3 + version: 3.2.3 typescript: specifier: 5.1.3 version: 5.1.3 @@ -1468,8 +1471,9 @@ packages: semver: 7.5.1 well-known-symbols: 2.0.0 - /consola@3.1.0: - resolution: {integrity: sha512-rrrJE6rP0qzl/Srg+C9x/AE5Kxfux7reVm1Wh0wCjuXvih6DqZgqDZe8auTD28fzJ9TF0mHlSDrPpWlujQRo1Q==} + /consola@3.2.3: + resolution: {integrity: sha512-I5qxpzLv+sJhTVEoLYNcTW+bThDCPsit0vLNKShZx6rLtpilNpmmeTPaeqJb9ZE9dV3DGaeby6Vuhrw38WjeyQ==} + engines: {node: ^14.18.0 || >=16.10.0} dev: true /convert-source-map@1.8.0: @@ -4314,7 +4318,7 @@ packages: '@rollup/plugin-replace': 5.0.2(rollup@3.20.2) '@rollup/pluginutils': 5.0.2(rollup@3.20.2) chalk: 5.2.0 - consola: 3.1.0 + consola: 3.2.3 defu: 6.1.2 esbuild: 0.17.19 globby: 13.1.4