-
-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathlinkedin.js
57 lines (51 loc) · 1.3 KB
/
linkedin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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: '<CLIENT_ID>',
secret: '<CLIENT_SECRET>'
},
auth: oauthPlugin.LINKEDIN_CONFIGURATION
},
tokenRequestParams: {
client_id: '<CLIENT_ID>',
client_secret: '<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 })