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.

How to properly wait (via Promise) on a JWT token?

See original GitHub issue

I’m using AWS Cognito for authentication, and I’m really confused about how to set it up so AppSync auth waits for Cognito authorization. I have this:

const client = new AWSAppSyncClient(
  {
    url: process.env.APPSYNC_API_URL,
    region: process.env.AWS_REGION,
    auth: {
      type: AUTH_TYPE.AMAZON_COGNITO_USER_POOLS,
      jwtToken: () => store.state.auth.user.idToken,
    },
  },
  {
    defaultOptions: {
      watchQuery: {
        fetchPolicy: 'cache-and-network',
      },
    },
  }
);

This works in most cases, but sometimes there’s a race condition when the AppSync client is ready but the Vuex store doesn’t have the token just yet. Any advice?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

5reactions
mikeparisstuffcommented, Feb 24, 2018

Hi,

You can provide an async function that returns a promise containing the token. Here is how you would do it using the Auth module from Amplify:

const client = new AWSAppSyncClient(
  {
    url: process.env.APPSYNC_API_URL,
    region: process.env.AWS_REGION,
    auth: {
      type: AUTH_TYPE.AMAZON_COGNITO_USER_POOLS,
      jwtToken: async () => (await Auth.currentSession()).getIdToken().getJwtToken(),
    },
  },
  {
    defaultOptions: {
      watchQuery: {
        fetchPolicy: 'cache-and-network',
      },
    },
  }
);

Hope this helps 😃

2reactions
menkveldjcommented, Mar 5, 2018

I might suggest adding promise resolver function in case of error. This will prevent anything from being printed to the console.log.

 jwtToken: async () => (await Auth.currentSession()
        .then(data => {
          return data
        })
        .catch(err => {
          return err
        })).getIdToken().getJwtToken(),
Read more comments on GitHub >

github_iconTop Results From Across the Web

the tokenGetter method does not wait for the promise to be ...
I have a getToken() method which is checking for token's validity and calling the service for getting new set of tokens. The method...
Read more >
Securing Node.js RESTful APIs with JSON Web Tokens(JWT ...
Securing Node.js RESTful APIs using JSON Web Tokens with great ... Try Catch the awaited promise to handle the error try { var...
Read more >
Handling JWT in Admin Apps the Right Way - Marmelab
The solution will consist in asking these methods to wait for the end of the possible fetch to the /refresh-token endpoint started by...
Read more >
Node js http request await - viviamaviaggia.it
Handling Axios promises using async-await. request to send the data to ... Node js jwt authentication mysql Step 2: Return a JWT token...
Read more >
Learn how to handle authentication with Node.js using JWT
JWT (JSON Web Token) is usually used to send information that can be trusted and verified using a digital signature. A user sends...
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