question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Refresh a token (Multiple Oauth2Clients)

See original GitHub issue

So, i was reading the docs and i saw the only way to refresh a token is this

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);
});

Isn’t it inefficient to add an event to every single client? isn’t there a better way to manually check the clients access token and refresh it using the refresh token?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:14 (2 by maintainers)

github_iconTop GitHub Comments

8reactions
mrdulincommented, Jul 19, 2019

@richardpeng I agree.

@bcoe The tokens event can’t access application context. Where should I put this .on("tokens") in application code?

For example, if use express, the context info is attached in req object, like req.user

I think below way is wrong:

app.post('/post', (req, res) => {
  auth.on('tokens', async (tokens) => {
    const requestingUser = req.user
    const user = await db.findByEmail(requestingUser.email)
    await db.updateGoogleAccountTokensByUser(user)
  })
})

If put auth.on('tokens', (tokens) => {}) on a global environment

auth.on('tokens', (tokens) => {
  // Don't know whose tokens
  const user = ??
  await db.updateGoogleAccountTokensByUser(user)
})
3reactions
fabiofcferreiracommented, Feb 25, 2019

Hey! I randomly came across this issue and I actually wanted to ask something very similar to @supertiger1234 asked.

What if I want to save the new token to the database? Using the listener becomes very hard in that matter. What is your suggestion?

Read more comments on GitHub >

github_iconTop Results From Across the Web

how many refresh tokens and access tokens can one single ...
With OAuth2 a clientID will only have 1 active set of tokens (access+refresh). If you have multiple ClientID's from the API you can...
Read more >
OAuth with PHP Part Two: refreshing & revoking tokens
Here is what it looks like with PHP using file_get_contents code: //Refresh the token $url = "[https://connect.squareup.com/oauth2/clients/$ ...
Read more >
How to Automatically Request and Refresh OAuth2 Client ...
The response from the authorization server includes an access token which then must be passed on the request to Order Service in order...
Read more >
Refresh Tokens - ForgeRock Backstage
Refresh tokens (RFC 6749) are a type of token that can be used to obtain a new access token that may have identical...
Read more >
Multiple valid refresh tokens at the same time. - Microsoft Q&A
Multiple valid refresh tokens at the same time. I got a token and a refresh token following https://docs.microsoft.com/en-us/azure/active- ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found