File tree Expand file tree Collapse file tree 2 files changed +53
-0
lines changed Expand file tree Collapse file tree 2 files changed +53
-0
lines changed Original file line number Diff line number Diff line change
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 )
Original file line number Diff line number Diff line change @@ -126,4 +126,11 @@ oauthPlugin.LINKEDIN_CONFIGURATION = {
126
126
tokenPath : '/oauth/v2/accessToken'
127
127
}
128
128
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
+
129
136
module . exports = oauthPlugin
You can’t perform that action at this time.
0 commit comments