From 11b404276a54b7d1ea13e29d1b05fb6472bd5559 Mon Sep 17 00:00:00 2001 From: TAB_mk Date: Wed, 25 Sep 2019 00:18:19 +0300 Subject: [PATCH 1/2] vkontakte aka vk.com example --- examples/vkontakte.js | 31 +++++++++++++++++++++++++++++++ index.js | 7 +++++++ 2 files changed, 38 insertions(+) create mode 100644 examples/vkontakte.js diff --git a/examples/vkontakte.js b/examples/vkontakte.js new file mode 100644 index 0000000..1a4bd10 --- /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 From 94d165510f61dc935d674d50bf5c247f83f2baef Mon Sep 17 00:00:00 2001 From: TAB_mk Date: Wed, 25 Sep 2019 00:31:17 +0300 Subject: [PATCH 2/2] build fixes --- examples/vkontakte.js | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/examples/vkontakte.js b/examples/vkontakte.js index 1a4bd10..19b90cd 100644 --- a/examples/vkontakte.js +++ b/examples/vkontakte.js @@ -1,31 +1,31 @@ -const fastify = require('fastify')({ logger: { level: 'trace' } }); -const oauthPlugin = require('fastify-oauth2'); +const fastify = require('fastify')({ logger: { level: 'trace' } }) +const oauthPlugin = require('fastify-oauth2') fastify.register(oauthPlugin, { - name : 'vkOAuth2', - scope : ['email'], + name: 'vkOAuth2', + scope: ['email'], credentials: { client: { - id : process.env.CLIENT_ID, - secret : process.env.CLIENT_SECRET + id: process.env.CLIENT_ID, + secret: process.env.CLIENT_SECRET }, - auth : oauthPlugin.VKONTAKTE_CONFIGURATION + 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); + const token = await fastify.getAccessTokenFromAuthorizationCodeFlow(req) - console.log(token); - reply.send({ access_token: token.access_token }); -}); + 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.error(err) + process.exit(1) } - fastify.log.info(`server listening on ${address}`); -}); + fastify.log.info(`server listening on ${address}`) +})