Skip to content

Commit 1197cfa

Browse files
committed
Add Google login example
1 parent 2f28101 commit 1197cfa

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

examples/google.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
'use strict'
2+
3+
const fastify = require('fastify')({ logger: { level: 'trace' } })
4+
const sget = require('simple-get')
5+
6+
const oauthPlugin = require('..')
7+
8+
fastify.register(oauthPlugin, {
9+
name: 'googleOAuth2',
10+
scope: ['profile'],
11+
credentials: {
12+
client: {
13+
id: '<CLIENT_ID>',
14+
secret: '<CLIENT_SECRET>'
15+
},
16+
auth: oauthPlugin.GOOGLE_CONFIGURATION
17+
},
18+
startRedirectPath: '/login/google',
19+
callbackUri: 'http://localhost:3000/login/google/callback'
20+
})
21+
22+
fastify.get('/login/google/callback', function (request, reply) {
23+
this.getAccessTokenFromAuthorizationCodeFlow(request, (err, result) => {
24+
if (err) {
25+
reply.send(err)
26+
return
27+
}
28+
29+
sget.concat({
30+
url: 'https://www.googleapis.com/plus/v1/people/me',
31+
method: 'GET',
32+
headers: {
33+
Authorization: 'Bearer ' + result.access_token
34+
},
35+
json: true
36+
}, function (err, res, data) {
37+
if (err) {
38+
reply.send(err)
39+
return
40+
}
41+
reply.send(data)
42+
})
43+
})
44+
})
45+
46+
fastify.listen(3000)

0 commit comments

Comments
 (0)