diff --git a/examples/google.js b/examples/google.js new file mode 100644 index 0000000..429d90d --- /dev/null +++ b/examples/google.js @@ -0,0 +1,46 @@ +'use strict' + +const fastify = require('fastify')({ logger: { level: 'trace' } }) +const sget = require('simple-get') + +const oauthPlugin = require('..') + +fastify.register(oauthPlugin, { + name: 'googleOAuth2', + scope: ['profile'], + credentials: { + client: { + id: '', + secret: '' + }, + auth: oauthPlugin.GOOGLE_CONFIGURATION + }, + startRedirectPath: '/login/google', + callbackUri: 'http://localhost:3000/login/google/callback' +}) + +fastify.get('/login/google/callback', function (request, reply) { + this.getAccessTokenFromAuthorizationCodeFlow(request, (err, result) => { + if (err) { + reply.send(err) + return + } + + sget.concat({ + url: 'https://www.googleapis.com/plus/v1/people/me', + method: 'GET', + headers: { + Authorization: 'Bearer ' + result.access_token + }, + json: true + }, function (err, res, data) { + if (err) { + reply.send(err) + return + } + reply.send(data) + }) + }) +}) + +fastify.listen(3000) diff --git a/index.js b/index.js index f177a8f..7d13d33 100644 --- a/index.js +++ b/index.js @@ -126,4 +126,11 @@ oauthPlugin.LINKEDIN_CONFIGURATION = { tokenPath: '/oauth/v2/accessToken' } +oauthPlugin.GOOGLE_CONFIGURATION = { + authorizeHost: 'https://accounts.google.com', + authorizePath: '/o/oauth2/v2/auth', + tokenHost: 'https://www.googleapis.com', + tokenPath: '/oauth2/v4/token' +} + module.exports = oauthPlugin