passport-google-oauth2 icon indicating copy to clipboard operation
passport-google-oauth2 copied to clipboard

dynamic callbackURL

Open clocked0ne opened this issue 8 years ago • 3 comments

Hi,

Thank you for releasing an 'official' Google Oauth2 passport module. I am having some trouble trying to implement a dynamic callback URL arrangement similar to that discussed here: https://github.com/jaredhanson/passport-facebook/issues/2

I have two auth routes available, with two callback routes (which are also specified in my Google account):

    app.get('/auth/google', passport.authenticate('google', { scope: scope,
        accessType: 'offline'
    }));
    app.get('/auth/google/calendar', passport.authenticate('google', { scope: calendarScope,
        accessType: 'offline',
        callbackURL: '/auth/google/callback/calendar'
    }));

    app.get('/auth/google/callback', passport.authenticate( 'google', {
        successRedirect: '/dashboard',
        failureRedirect: '/'
    }));
    app.get('/auth/google/callback/calendar', passport.authenticate( 'google', {
        successRedirect: '/dashboard/applications?calendar=yes',
        failureRedirect: '/dashboard/applications?calendar=no'
    }));

But this does not work, no matter what combination of methods I try, I always receive the following error:

500

TokenError: Bad Request
    at Strategy.OAuth2Strategy.parseErrorResponse (/opt/.../node_modules/passport-oauth2/lib/strategy.js:320:12)
    at Strategy.OAuth2Strategy._createOAuthError (/opt/.../node_modules/passport-oauth2/lib/strategy.js:367:16)
    at /opt/.../node_modules/passport-oauth2/lib/strategy.js:166:45
    at /opt/.../node_modules/oauth/lib/oauth2.js:177:18
    at passBackControl (/opt/.../node_modules/oauth/lib/oauth2.js:123:9)
    at IncomingMessage.<anonymous> (/opt/.../node_modules/oauth/lib/oauth2.js:143:7)
    at emitNone (events.js:85:20)
    at IncomingMessage.emit (events.js:179:7)
    at endReadableNT (_stream_readable.js:913:12)
    at _combinedTickCallback (internal/process/next_tick.js:74:11)
    at process._tickCallback (internal/process/next_tick.js:98:9)

clocked0ne avatar Apr 18 '16 16:04 clocked0ne

logging the error returned from the passport-oauth2 dependency it would appear the root cause is:

{ statusCode: 400,
  data: '{ "error": "redirect_uri_mismatch", "error_description": "Bad Request" }' }

So it would appear that specifying a different callbackURL in the options of passport.authenticate does not work for this particular Strategy?

As this has been previously added to passport-facebook would it be possible to add the same to passport-google-oauth20?

clocked0ne avatar Apr 20 '16 10:04 clocked0ne

In conclusion, the feature is there and working, however it is not explicit in how it works.

You don't need to define the callbackURL parameter in the configuration of the Google Strategy any more if you want to use it as a dynamic paramater, but it must be specified in every options object passed to passport.authenticate.

I would be happy to raise a pull request to update the Readme with some explanation and an example of this feature to raise awareness, if you like?

clocked0ne avatar Apr 20 '16 10:04 clocked0ne

First check if you're GoogleSrategy object is correct and then within the object, check the callbakURL, I agree with clockedOne .. passport.js needs to use callbackURL

new GoogleStrategy({ // options for the google start callbackURL:'"Path"', clientID: clientID, clientSecret:clientSecret },() => { // this is the passport callback function test it out console.log('if you are seeing this youre passport callback function fired now use the infor to store and validate the user 👍 '); }) )

brunomiyamottol avatar Dec 03 '17 18:12 brunomiyamottol