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.

Appsync Authentication - AWS Cognito User Pools with AWS Amplify

See original GitHub issue

To pass the jwtToken in auth object https://www.youtube.com/watch?v=vAjf3lyjf8c&feature=youtu.be&t=3410 talks about passing it like:

screen shot 2018-01-06 at 1 48 07 pm

but Auth.currentSession returns a promise (not sure if above approach is correct) so I followed this approach:

screen shot 2018-01-06 at 1 49 09 pm

Now jwtToken is successfully generated but client auth object doesn’t wait for credentials to get generated.

What is the right way to generate token on the client side for cognito user pools?

Reference Implementation: https://github.com/serverless/serverless-graphql/pull/227

Amplify.configure({
  Auth: {
    region: process.env.REACT_APP_AWS_AUTH_REGION, // REQUIRED - Amazon Cognito Region
    userPoolId: process.env.REACT_APP_USER_POOL_ID, //OPTIONAL - Amazon Cognito User Pool ID
    userPoolWebClientId: process.env.REACT_APP_CLIENT_APP_ID, //User Pool App Client ID
  },
});

const getCreds = async () => {
  return await Auth.currentSession()
    .then(data => {
      return data.idToken.jwtToken;
    })
    .catch(err => console.log(err));
};

const client = new AWSAppSyncClient({
  url: process.env.REACT_APP_GRAPHQL_ENDPOINT,
  region: process.env.REACT_APP_AWS_CLIENT_REGION,
  auth: {
    type: AUTH_TYPE.AMAZON_COGNITO_USER_POOLS,
    jwtToken: getCreds(),
  },
});```

Issue Analytics

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

github_iconTop GitHub Comments

20reactions
manueliglesiascommented, Feb 13, 2018

Hey @sid88in and @lolcoolkat with the latest aws-amplify and aws-appsync you can do something like this:

// AppSync client instantiation
const client = new AWSAppSyncClient({
  url: AppSync.graphqlEndpoint,
  region: AppSync.region,
  auth: {
    // IAM
    // type: AUTH_TYPE.AWS_IAM,
    // credentials: () => Auth.currentCredentials(),

    // COGNITO USER POOLS
    type: AUTH_TYPE.AMAZON_COGNITO_USER_POOLS,
    jwtToken: async () => (await Auth.currentSession()).getAcceessToken().getJwtToken(),
  },
});

Hope that helps

3reactions
yalambercommented, Aug 13, 2018

@manueliglesias it should probably be getAccessToken() instead of getAcceessToken() just a small typo

Read more comments on GitHub >

github_iconTop Results From Across the Web

Authorization and authentication - AWS AppSync
This authorization type enforces OIDC tokens provided by Amazon Cognito User Pools. Your application can leverage the users and groups in your user...
Read more >
How to connect to AWS AppSync using Cognito Identity Pools ...
I'm using the Amplify GraphQL client to connect to the AppSync server. The image below describes the steps that need to take place...
Read more >
API (GraphQL) - Configure authorization modes - Swift
You can configure auth modes for an API using the Amplify CLI or manual ... Amazon Cognito User Pools is most commonly used...
Read more >
Accessing AppSync APIs that require Cognito Login outside of ...
One way to protect your AppSync data is to use Cognito Identity Pools. Amplify makes it pretty transparent if you are using Amplify...
Read more >
How to use Cognito with AppSync - Advanced Web Machinery
AWS AppSync provides a managed GraphQL API that you can use to implement backend functionality for users. AppSync integrates with Cognito ...
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