-
-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathfacebook.js
More file actions
44 lines (36 loc) · 1.05 KB
/
facebook.js
File metadata and controls
44 lines (36 loc) · 1.05 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
'use strict'
const fastify = require('fastify')({ logger: { level: 'trace' } })
// 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, async (err, result) => {
if (err) {
reply.send(err)
return
}
const fetchResult = await fetch('https://graph.facebook.com/v6.0/me', {
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 })