Skip to content

Commit 168ec7c

Browse files
authored
Merge pull request #33 from fastify/spotify-conf
add spotify config
2 parents 4b2c3fb + 91fa871 commit 168ec7c

File tree

3 files changed

+74
-1
lines changed

3 files changed

+74
-1
lines changed

README.md

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,45 @@ fastify.get('/login/facebook/callback', async function (request, reply) {
4242
})
4343
```
4444

45+
### Preset configurations
46+
47+
You can choose some default setup to assign to `auth` option.
48+
49+
- `FACEBOOK_CONFIGURATION`
50+
- `GITHUB_CONFIGURATION`
51+
- `LINKEDIN_CONFIGURATION`
52+
- `GOOGLE_CONFIGURATION`
53+
- `MICROSOFT_CONFIGURATION`
54+
- `VKONTAKTE_CONFIGURATION`
55+
- `SPOTIFY_CONFIGURATION`
56+
57+
### Custom configuration
58+
59+
Of course you can set the OAUTH endpoints by yourself if a preset is not in our module:
60+
61+
```js
62+
fastify.register(oauthPlugin, {
63+
name: 'customOauth2',
64+
credentials: {
65+
client: {
66+
id: '<CLIENT_ID>',
67+
secret: '<CLIENT_SECRET>'
68+
},
69+
auth: {
70+
authorizeHost: 'https://my-site.com',
71+
authorizePath: '/authorize',
72+
tokenHost: 'https://token.my-site.com',
73+
tokenPath: '/api/token'
74+
}
75+
},
76+
startRedirectPath: '/login',
77+
callbackUri: 'http://localhost:3000/login/callback'
78+
})
79+
```
80+
4581
## Example
4682

47-
See [facebook example](./examples/facebook.js) for an example.
83+
See the [`example/`](./examples/) folder for more example.
4884

4985
## Reference
5086

examples/spotify.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const fastify = require('fastify')({ logger: true })
2+
const oauthPlugin = require('fastify-oauth2')
3+
4+
fastify.register(oauthPlugin, {
5+
name: 'Spotify',
6+
scope: ['user-read-currently-playing'],
7+
credentials: {
8+
client: {
9+
id: process.env.CLIENT_ID,
10+
secret: process.env.CLIENT_SECRET
11+
},
12+
auth: oauthPlugin.SPOTIFY_CONFIGURATION
13+
},
14+
startRedirectPath: '/login/spotify',
15+
callbackUri: `http://localhost:${process.env.PORT}/login/spotify/callback`
16+
})
17+
18+
fastify.get('/login/spotify/callback', async (req, reply) => {
19+
const token = await fastify.getAccessTokenFromAuthorizationCodeFlow(req)
20+
21+
req.log.info('The Spotify token is %o', token)
22+
reply.send({ access_token: token.access_token })
23+
})
24+
25+
fastify.listen(process.env.PORT, (err) => {
26+
if (err) {
27+
fastify.log.error(err)
28+
process.exit(1)
29+
}
30+
})

index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,4 +166,11 @@ oauthPlugin.VKONTAKTE_CONFIGURATION = {
166166
tokenPath: '/access_token'
167167
}
168168

169+
oauthPlugin.SPOTIFY_CONFIGURATION = {
170+
authorizeHost: 'https://accounts.spotify.com',
171+
authorizePath: '/authorize',
172+
tokenHost: 'https://accounts.spotify.com',
173+
tokenPath: '/api/token'
174+
}
175+
169176
module.exports = oauthPlugin

0 commit comments

Comments
 (0)