Google-api-nodejs-client: Guidance on saving access/refresh tokens leads to redundant writes

Created on 23 Jul 2019  路  2Comments  路  Source: googleapis/google-api-nodejs-client

OK.. so my title is a bit click-baity, but still...

The guidance in the readme (ref: Retrieve access token), suggests:

// This will provide an object with the access_token and refresh_token.
// Save these somewhere safe so they can be used at a later time.
const {tokens} = await oauth2Client.getToken(code)
oauth2Client.setCredentials(tokens);

Saving this token to a database (for example), will result in a duplicate write if we _also_ follow the guidance for refresh tokens (ref: Handling refresh tokens), which suggests:

oauth2Client.on('tokens', (tokens) => {
  if (tokens.refresh_token) {
    // store the refresh_token in my database!
    console.log(tokens.refresh_token);
  }
  console.log(tokens.access_token);
});

It seems that oauth2Client.on('tokens') is called when setting credentials on the client, in which case it's called in the first retrieval of the access token, which then also triggers a call to this method via the setCredentials invocation.

_If_ oauth2Client.on('tokens') is _always_ called when credentials are set, then maybe it makes sense to change the guidance to only/always use that to store tokens

docs

Most helpful comment

Follow up.. I created a sample app that demonstrates the problem

https://github.com/jasonpolites/google-oauth-test

Note it's actually worse than just double-writes, in that there exists a race condition which leads to double OAuth dances. Though I might be holding it wrong

All 2 comments

Follow up.. I created a sample app that demonstrates the problem

https://github.com/jasonpolites/google-oauth-test

Note it's actually worse than just double-writes, in that there exists a race condition which leads to double OAuth dances. Though I might be holding it wrong

Greetings, we're closing this due to inactivity. Please let us know if the issue needs to be reopened.

Was this page helpful?
0 / 5 - 0 ratings