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

Refresh the token in an AWS Lambda function #1379

Closed
baptisteArno opened this issue Mar 4, 2022 · 2 comments
Closed

Refresh the token in an AWS Lambda function #1379

baptisteArno opened this issue Mar 4, 2022 · 2 comments
Assignees
Labels
priority: p3 Desirable enhancement or fix. May not be included in next release. type: question Request for information or clarification. Not an issue.

Comments

@baptisteArno
Copy link

baptisteArno commented Mar 4, 2022

The library works perfectly on my machine but when it's deployed in a Lambda function, the token doesn't seem to automatically update:

export const getAuthenticatedGoogleClient = async (
  userId: string,
  credentialsId: string
): Promise<OAuth2Client | undefined> => {
  const credentials = (await prisma.credentials.findFirst({
    where: { id: credentialsId, ownerId: userId },
  })) as CredentialsFromDb | undefined
  oauth2Client.setCredentials(credentials)
  oauth2Client.on('tokens', updateTokens(credentialsId))
  return oauth2Client
}

const updateTokens = (credentialsId: string) => async (credentials) =>
   prisma.credentials.update({
      where: { id: credentialsId },
      data: { credentials },
    })

My guess is that it doesn't have time to update in the database because this is handled in a callback.

Is there a way to check if expiry_date is passed and update the token with a Promise-based function?

Some examples of errors I get:
CleanShot 2022-03-04 at 21 07 02@2x

@baptisteArno baptisteArno added priority: p3 Desirable enhancement or fix. May not be included in next release. type: question Request for information or clarification. Not an issue. labels Mar 4, 2022
@baptisteArno
Copy link
Author

Oooooh I think I know, I think the tokens from the callback don't contain the refresh_token so I pushed credentials in db without the refresh_token!

@danielbankhead danielbankhead self-assigned this Sep 29, 2023
@danielbankhead
Copy link
Member

Hey, apologies for the delay - I would recommend storing the refresh_token when it is available in the callback (usually, the initial authorization):

Here's some documentation as well: https://github.com/googleapis/google-auth-library-nodejs#handling-token-events

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority: p3 Desirable enhancement or fix. May not be included in next release. type: question Request for information or clarification. Not an issue.
Projects
None yet
Development

No branches or pull requests

2 participants