-
-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathlinkedin.js
More file actions
52 lines (44 loc) · 1.23 KB
/
linkedin.js
File metadata and controls
52 lines (44 loc) · 1.23 KB
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
'use strict'
const fastify = require('fastify')({ logger: { level: 'trace' } })
// 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,
async (err, result) => {
if (err) {
reply.send(err)
return
}
const fetchResult = await fetch('https://api.linkedin.com/v2/userinfo', {
headers: {
Authorization: 'Bearer ' + result.token.access_token
}
})
if (!fetchResult.ok) {
reply.send(new Error('Failed to fetch user info'))
return
}
const data = await fetchResult.json()
reply.send(data)
}
)
})
fastify.listen({ port: 3000 })