Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unnecessary second network request #20

Open
derN3rd opened this issue Dec 11, 2017 · 0 comments
Open

Unnecessary second network request #20

derN3rd opened this issue Dec 11, 2017 · 0 comments

Comments

@derN3rd
Copy link

derN3rd commented Dec 11, 2017

As Jared already said in

// TODO: Instagram provides user profile information in the access token
// response. As an optimization, that information should be used, which
// would avoid the need for an extra request during this step. However,
// the internal node-oauth module will have to be modified to support
// exposing this information.
we don't need to make a second call to the Instagram API here if we just need basic information.

But we don't have to rewrite the oauth2 module, because it already gives us the data we need. So if you only need the basic information, you can avoid a second network request with the following trick:

pass skipUserProfile: true to the options and add one parameter to the callback function, as shown in the following example:

Before:

passport.use(
  new InstagramStrategy(
    {
      clientID: config.instagram.clientID,
      clientSecret: config.instagram.clientSecret,
      callbackURL: `https://${siteurl}/auth/instagram/callback`,
      passReqToCallback: true,
    },
    (req, accessToken, refreshToken, profile, done) => {
      return doRegisterOrLogin(req, profile, done)
    }
  )
)

After:

passport.use(
  new InstagramStrategy(
    {
      clientID: config.instagram.clientID,
      clientSecret: config.instagram.clientSecret,
      callbackURL: `https://${siteurl}/auth/instagram/callback`,
      passReqToCallback: true,
      skipUserProfile: true,
    },
    (req, accessToken, refreshToken, params, _profile, done) => {
      console.log(params)
      if (typeof params === 'undefined' || params === null || params === {}) {
        return done(new Error('invalid data from instagram'))
      }
      const profile = params.user

      return doRegisterOrLogin(req, profile, done)
    }
  )
)

As you can see, we can skip the second request and the params object looks something similar to this:

{ access_token: '11970824.2*****0.37f*****e5274***a51643*****abb**',
  user:
   { id: '11970824',
     username: 'dern3rd',
     profile_picture: 'https://...._n.jpg',
     full_name: 'Max',
     bio: 'bio goes here',
     website: 'https://domain.tld',
     is_business: false } }

This helped us a lot, because many requests (around 4%) failed on the second request (because of 'internal server error's from instagram)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant