Comments (10)
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.
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.
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.
@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.
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.
from aws-mobile-appsync-sdk-js.
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.
Thanks, Manuel!
from aws-mobile-appsync-sdk-js.
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.
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)
- Getting apollo-link Type error on build HOT 2
- Subscriptions are disconnected after background/lock and unlock the app in React-Native HOT 8
- Type 'CredentialsGetter | undefined' is not assignable to type 'CredentialsGetter' with createAppSyncLink
- TypeError: Cannot read properties of null (reading 'match') HOT 2
- aws-appsync-react not installing in React 18 HOT 3
- Buffer is now only available as a polyfill on the new vite builds. Module "buffer" has been externalized for browser compatibility HOT 1
- Getting "The request signature we calculated does not match the signature you provided." when creating subscriptions using aws-appsync-subscription-link and apollo client v3 HOT 3
- undefined Unable to resolve module stream from
- Spread operator does not work with util.appendError HOT 1
- npm install fails with typescript error
- deepdish:connect errors with token expiry.
- Error: Unable to resolve module AccessibilityInfo
- Vanilla AWS SDK v3 + Apollo V3 query guide
- Managing Reconnection Attempts for AWS AppSync Subscription with Apollo
- useSubscription does not conform to Apollo HOT 2
- buildMutation not working properly with header
- Passing a header into AWSAppSyncClient?
- Console showing error
- Refused to set unsafe header "host" with IAM auth link
- Module size optimization for production
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from aws-mobile-appsync-sdk-js.