Giter Club home page Giter Club logo

Comments (10)

mikeparisstuff avatar mikeparisstuff commented on May 12, 2024 5

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 :)

from aws-mobile-appsync-sdk-js.

menkveldj avatar menkveldj commented on May 12, 2024 3

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(),

from aws-mobile-appsync-sdk-js.

manueliglesias avatar manueliglesias commented on May 12, 2024 1

Hey @ffxsam

Ah yes.. right. I guess it makes no sense for me to fetch that from the Vuex store.

Right, things will be easier for you if you read the token using amplify

Here's the next issue: when Amplify refreshes the user's tokens, how will AppSync pick up on that?

For every single request, the AppSync client will await for the promise you return in

jwtToken: async () => (await Auth.currentSession()).getIdToken().getJwtToken()

That jwtToken function will be executed once per request and the token will be always fresh (Amplify internally handles the refresh for you)

from aws-mobile-appsync-sdk-js.

ffxsam avatar ffxsam commented on May 12, 2024 1

@cdcv You didn't have to dig too far to get the right answer:

https://aws-amplify.github.io/docs/js/api#cognito-user-pools-auth

import Amplify, { Auth } from 'aws-amplify';
import awsconfig from './aws-exports';

Amplify.configure(awsconfig);

const client = new AWSAppSyncClient({
  url: awsconfig.aws_appsync_graphqlEndpoint,
  region: awsconfig.aws_appsync_region,
  auth: {
    type: AUTH_TYPE.AMAZON_COGNITO_USER_POOLS,
    jwtToken: async () => (await Auth.currentSession()).getIdToken().getJwtToken(),
  },
});

It's documented. Their code is a bit different from yours, so I'd try that instead.

from aws-mobile-appsync-sdk-js.

ffxsam avatar ffxsam commented on May 12, 2024

Ah yes.. right. I guess it makes no sense for me to fetch that from the Vuex store.

Here's the next issue: when Amplify refreshes the user's tokens, how will AppSync pick up on that?

from aws-mobile-appsync-sdk-js.

sid88in avatar sid88in commented on May 12, 2024

@ffxsam #49 might be helpful

from aws-mobile-appsync-sdk-js.

ffxsam avatar ffxsam commented on May 12, 2024

I'm not sure how that helps. Amplify supposedly auto refreshes the tokens for you. How can I know when that happens? How do I update the JWT token in AppSync?

from aws-mobile-appsync-sdk-js.

ffxsam avatar ffxsam commented on May 12, 2024

Thanks, Manuel!

from aws-mobile-appsync-sdk-js.

cdcv avatar cdcv commented on May 12, 2024

After a LONG time trying to figure out where a random error that appeared and crashed our app was, I did this as per your suggestion (thanks @menkveldj):

const client = new AWSAppSyncClient({
url: aws_config.aws_appsync_graphqlEndpoint,
region: aws_config.aws_appsync_region,
auth: {
type: AUTH_TYPE.AMAZON_COGNITO_USER_POOLS,
// type: AUTH_TYPE.API_KEY,
// apiKey: aws_config.aws_appsync_apiKey
jwtToken: async () => (await Auth.currentSession()
.then(data => {
return data
})
.catch(err => {
console.log('AWS sux my arse because:',err);
return err
})).getIdToken().getJwtToken()
}
})

However, this doesn't fully solve the problem.
Now I'm getting:
bundle.js:20121 Uncaught (in promise) TypeError: (intermediate value).getIdToken is not a function. I'm not sure how to handing the error case correctly. Thank you for your help.

AWS: Please consider making your product actually work / documenting it adequately to be useable. Reading the many postings on this technology, it's clear that many people have spent many days (like we have) trying to get the very most basic of all functionality (authentication to be able to use it at all) to work using AppSync. We certainly wish that we had never used it at all.

from aws-mobile-appsync-sdk-js.

cdcv avatar cdcv commented on May 12, 2024

Yes @ffxsam that's the code that we started with, which caused all the problems, is directly out of their documentation, and does not work / causes the app to crash because it does not contain adequate error checking and it throws errors.

from aws-mobile-appsync-sdk-js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.