diff --git a/examples/vkontakte.js b/examples/vkontakte.js new file mode 100644 index 0000000..19b90cd --- /dev/null +++ b/examples/vkontakte.js @@ -0,0 +1,31 @@ +const fastify = require('fastify')({ logger: { level: 'trace' } }) +const oauthPlugin = require('fastify-oauth2') + +fastify.register(oauthPlugin, { + name: 'vkOAuth2', + scope: ['email'], + credentials: { + client: { + id: process.env.CLIENT_ID, + secret: process.env.CLIENT_SECRET + }, + auth: oauthPlugin.VKONTAKTE_CONFIGURATION + }, + startRedirectPath: '/login/vk', + callbackUri: `http://localhost:${process.env.PORT}/login/vk/callback` +}) + +fastify.get('/login/vk/callback', async (req, reply) => { + const token = await fastify.getAccessTokenFromAuthorizationCodeFlow(req) + + console.log(token) + reply.send({ access_token: token.access_token }) +}) + +fastify.listen(process.env.PORT, (err, address) => { + if (err) { + fastify.log.error(err) + process.exit(1) + } + fastify.log.info(`server listening on ${address}`) +}) diff --git a/index.js b/index.js index e1f9ba4..8da3feb 100644 --- a/index.js +++ b/index.js @@ -159,4 +159,11 @@ oauthPlugin.MICROSOFT_CONFIGURATION = { tokenPath: '/common/oauth2/v2.0/token' } +oauthPlugin.VKONTAKTE_CONFIGURATION = { + authorizeHost: 'https://oauth.vk.com', + authorizePath: '/authorize', + tokenHost: 'https://oauth.vk.com', + tokenPath: '/access_token' +} + module.exports = oauthPlugin