forked from fastify/fastify-oauth2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfacebook.js
46 lines (40 loc) · 1.06 KB
/
facebook.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
'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: 'facebookOAuth2',
credentials: {
client: {
id: '<CLIENT_ID>',
secret: '<CLIENT_SECRET>'
},
auth: oauthPlugin.FACEBOOK_CONFIGURATION
},
startRedirectPath: '/login/facebook',
callbackUri: 'http://localhost:3000/login/facebook/callback'
})
fastify.get('/login/facebook/callback', function (request, reply) {
this.facebookOAuth2.getAccessTokenFromAuthorizationCodeFlow(request, (err, result) => {
if (err) {
reply.send(err)
return
}
sget.concat({
url: 'https://graph.facebook.com/v6.0/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({ port: 3000 })