Skip to content

Commit 3e36377

Browse files
authored
Merge pull request #29 from TABmk/master
vk.com example
2 parents 86862d1 + 94d1655 commit 3e36377

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

examples/vkontakte.js

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

index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,4 +159,11 @@ oauthPlugin.MICROSOFT_CONFIGURATION = {
159159
tokenPath: '/common/oauth2/v2.0/token'
160160
}
161161

162+
oauthPlugin.VKONTAKTE_CONFIGURATION = {
163+
authorizeHost: 'https://oauth.vk.com',
164+
authorizePath: '/authorize',
165+
tokenHost: 'https://oauth.vk.com',
166+
tokenPath: '/access_token'
167+
}
168+
162169
module.exports = oauthPlugin

0 commit comments

Comments
 (0)