From b3399521192bda400976144f8e1c7f35afd061d0 Mon Sep 17 00:00:00 2001 From: Luca Fregoso Date: Wed, 20 Mar 2024 23:16:02 +0100 Subject: [PATCH 1/2] Add linkedin authentication example ise tokenRequestParams to proceed with the correct flow with getAccessTokenFromAuthorizationCodeFlow --- examples/linkedin.js | 57 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 examples/linkedin.js diff --git a/examples/linkedin.js b/examples/linkedin.js new file mode 100644 index 0000000..750b366 --- /dev/null +++ b/examples/linkedin.js @@ -0,0 +1,57 @@ +"use strict"; + +const fastify = require("fastify")({ logger: { level: "trace" } }); +const sget = require("simple-get"); + +// const oauthPlugin = require('fastify-oauth2') +const oauthPlugin = require(".."); + +fastify.register(oauthPlugin, { + name: "linkedinOAuth2", + scope: ["profile", "email", "openid"], + credentials: { + client: { + id: "", + secret: "", + }, + auth: oauthPlugin.LINKEDIN_CONFIGURATION, + }, + tokenRequestParams: { + client_id: "", + client_secret: "", + }, + startRedirectPath: "/login/linkedin", + callbackUri: "http://localhost:3000/login/linkedin/callback", +}); + +fastify.get("/login/linkedin/callback", function (request, reply) { + this.linkedinOAuth2.getAccessTokenFromAuthorizationCodeFlow( + request, + (err, result) => { + if (err) { + reply.send(err); + return; + } + + sget.concat( + { + url: "https://api.linkedin.com/v2/userinfo", + method: "GET", + headers: { + Authorization: "Bearer " + result.token.access_token, + }, + json: true, + }, + function (err, res, data) { + if (err) { + reply.send(err); + return; + } + reply.send(data); + } + ); + } + ); +}); + +fastify.listen({ port: 3000 }); From 577d8e29485a5ec44264fc564e221fe7c329b935 Mon Sep 17 00:00:00 2001 From: Luca Fregoso Date: Thu, 21 Mar 2024 11:12:22 +0100 Subject: [PATCH 2/2] code format running lint:fix --- examples/linkedin.js | 56 ++++++++++++++++++++++---------------------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/examples/linkedin.js b/examples/linkedin.js index 750b366..5a6d63d 100644 --- a/examples/linkedin.js +++ b/examples/linkedin.js @@ -1,57 +1,57 @@ -"use strict"; +'use strict' -const fastify = require("fastify")({ logger: { level: "trace" } }); -const sget = require("simple-get"); +const fastify = require('fastify')({ logger: { level: 'trace' } }) +const sget = require('simple-get') // const oauthPlugin = require('fastify-oauth2') -const oauthPlugin = require(".."); +const oauthPlugin = require('..') fastify.register(oauthPlugin, { - name: "linkedinOAuth2", - scope: ["profile", "email", "openid"], + name: 'linkedinOAuth2', + scope: ['profile', 'email', 'openid'], credentials: { client: { - id: "", - secret: "", + id: '', + secret: '' }, - auth: oauthPlugin.LINKEDIN_CONFIGURATION, + auth: oauthPlugin.LINKEDIN_CONFIGURATION }, tokenRequestParams: { - client_id: "", - client_secret: "", + client_id: '', + client_secret: '' }, - startRedirectPath: "/login/linkedin", - callbackUri: "http://localhost:3000/login/linkedin/callback", -}); + startRedirectPath: '/login/linkedin', + callbackUri: 'http://localhost:3000/login/linkedin/callback' +}) -fastify.get("/login/linkedin/callback", function (request, reply) { +fastify.get('/login/linkedin/callback', function (request, reply) { this.linkedinOAuth2.getAccessTokenFromAuthorizationCodeFlow( request, (err, result) => { if (err) { - reply.send(err); - return; + reply.send(err) + return } sget.concat( { - url: "https://api.linkedin.com/v2/userinfo", - method: "GET", + url: 'https://api.linkedin.com/v2/userinfo', + method: 'GET', headers: { - Authorization: "Bearer " + result.token.access_token, + Authorization: 'Bearer ' + result.token.access_token }, - json: true, + json: true }, function (err, res, data) { if (err) { - reply.send(err); - return; + reply.send(err) + return } - reply.send(data); + reply.send(data) } - ); + ) } - ); -}); + ) +}) -fastify.listen({ port: 3000 }); +fastify.listen({ port: 3000 })