Skip to content

Commit 0b37b04

Browse files
authored
Merge pull request #10 from ShaunLWM/master
Add Google OAuth Login
2 parents 1b9d787 + 1197cfa commit 0b37b04

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-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)

index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,11 @@ oauthPlugin.LINKEDIN_CONFIGURATION = {
126126
tokenPath: '/oauth/v2/accessToken'
127127
}
128128

129+
oauthPlugin.GOOGLE_CONFIGURATION = {
130+
authorizeHost: 'https://accounts.google.com',
131+
authorizePath: '/o/oauth2/v2/auth',
132+
tokenHost: 'https://www.googleapis.com',
133+
tokenPath: '/oauth2/v4/token'
134+
}
135+
129136
module.exports = oauthPlugin

0 commit comments

Comments
 (0)