Skip to content

Commit e54bb8c

Browse files
authored
Add linkedin authentication example (#250)
* Add linkedin authentication example ise tokenRequestParams to proceed with the correct flow with getAccessTokenFromAuthorizationCodeFlow * code format running lint:fix
1 parent f9c4a5c commit e54bb8c

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

examples/linkedin.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
'use strict'
2+
3+
const fastify = require('fastify')({ logger: { level: 'trace' } })
4+
const sget = require('simple-get')
5+
6+
// const oauthPlugin = require('fastify-oauth2')
7+
const oauthPlugin = require('..')
8+
9+
fastify.register(oauthPlugin, {
10+
name: 'linkedinOAuth2',
11+
scope: ['profile', 'email', 'openid'],
12+
credentials: {
13+
client: {
14+
id: '<CLIENT_ID>',
15+
secret: '<CLIENT_SECRET>'
16+
},
17+
auth: oauthPlugin.LINKEDIN_CONFIGURATION
18+
},
19+
tokenRequestParams: {
20+
client_id: '<CLIENT_ID>',
21+
client_secret: '<CLIENT_SECRET>'
22+
},
23+
startRedirectPath: '/login/linkedin',
24+
callbackUri: 'http://localhost:3000/login/linkedin/callback'
25+
})
26+
27+
fastify.get('/login/linkedin/callback', function (request, reply) {
28+
this.linkedinOAuth2.getAccessTokenFromAuthorizationCodeFlow(
29+
request,
30+
(err, result) => {
31+
if (err) {
32+
reply.send(err)
33+
return
34+
}
35+
36+
sget.concat(
37+
{
38+
url: 'https://api.linkedin.com/v2/userinfo',
39+
method: 'GET',
40+
headers: {
41+
Authorization: 'Bearer ' + result.token.access_token
42+
},
43+
json: true
44+
},
45+
function (err, res, data) {
46+
if (err) {
47+
reply.send(err)
48+
return
49+
}
50+
reply.send(data)
51+
}
52+
)
53+
}
54+
)
55+
})
56+
57+
fastify.listen({ port: 3000 })

0 commit comments

Comments
 (0)