@@ -210,6 +210,51 @@ t.test('fastify-oauth2', t => {
210210 makeRequests ( t , fastify )
211211 } )
212212
213+ t . test ( 'callbackUri as function' , t => {
214+ const fastify = createFastify ( { logger : { level : 'silent' } } )
215+
216+ fastify . register ( fastifyOauth2 , {
217+ name : 'githubOAuth2' ,
218+ credentials : {
219+ client : {
220+ id : 'my-client-id' ,
221+ secret : 'my-secret'
222+ } ,
223+ auth : fastifyOauth2 . GITHUB_CONFIGURATION
224+ } ,
225+ startRedirectPath : '/login/github' ,
226+ callbackUri : req => `${ req . protocol } ://localhost:3000/callback` ,
227+ scope : [ 'notifications' ]
228+ } )
229+
230+ fastify . get ( '/' , function ( request , reply ) {
231+ if ( this . githubOAuth2 !== this . oauth2GithubOAuth2 ) {
232+ throw new Error ( 'Expected oauth2GithubOAuth2 to match githubOAuth2' )
233+ }
234+ this . githubOAuth2 . getAccessTokenFromAuthorizationCodeFlow ( request , ( err , result ) => {
235+ if ( err ) throw err
236+
237+ // attempts to refresh the token
238+ this . githubOAuth2 . getNewAccessTokenUsingRefreshToken ( result . token , undefined , ( err , result ) => {
239+ if ( err ) throw err
240+
241+ const newToken = result
242+
243+ reply . send ( {
244+ access_token : newToken . token . access_token ,
245+ refresh_token : newToken . token . refresh_token ,
246+ expires_in : newToken . token . expires_in ,
247+ token_type : newToken . token . token_type
248+ } )
249+ } )
250+ } )
251+ } )
252+
253+ t . teardown ( fastify . close . bind ( fastify ) )
254+
255+ makeRequests ( t , fastify )
256+ } )
257+
213258 t . test ( 'promise' , t => {
214259 const fastify = createFastify ( { logger : { level : 'silent' } } )
215260
@@ -1399,7 +1444,7 @@ t.test('options.credentials should be an object', t => {
13991444 } )
14001445} )
14011446
1402- t . test ( 'options.callbackUri should be an object ' , t => {
1447+ t . test ( 'options.callbackUri should be a string or a function ' , t => {
14031448 t . plan ( 1 )
14041449
14051450 const fastify = createFastify ( { logger : { level : 'silent' } } )
@@ -1415,7 +1460,7 @@ t.test('options.callbackUri should be an object', t => {
14151460 }
14161461 } )
14171462 . ready ( err => {
1418- t . strictSame ( err . message , 'options.callbackUri should be a string' )
1463+ t . strictSame ( err . message , 'options.callbackUri should be a string or a function ' )
14191464 } )
14201465} )
14211466
0 commit comments