Skip to content

Unnecessary second network request #20

@derN3rd

Description

@derN3rd

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions