File tree Expand file tree Collapse file tree 1 file changed +46
-0
lines changed Expand file tree Collapse file tree 1 file changed +46
-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 )
You can’t perform that action at this time.
0 commit comments