googleapis / googleapis/google-api-nodejs-client

Guidance on saving access/refresh tokens leads to redundant writes

Aperta
#1,767 2 commenti 4 reazioni 1 assegnatario Vedi su GitHub

@jasonpolites ci sta già lavorando.

Dal 27/2/2023.

size: m type: docs
Lingua principale
TypeScript
Stelle
12.3k
Fork
2k
Merge medio
1g 9h
PR unite (30g)
24

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.