Aws-mobile-appsync-sdk-js: How to update token in AWSAppSyncClient?

Created on 10 Jul 2018  路  3Comments  路  Source: awslabs/aws-mobile-appsync-sdk-js

There are lots of blog posts on how to initialize a client.
But they all show very same pattern - client is initialized in render method and jwt token is provided.
And no any examples on how to update a client if jwt token is received later.

Like this one https://itnext.io/integrating-amazon-cognito-authentication-with-the-aws-appsync-graphql-client-4282afb6eee1

render() {
  const Navigator = StackNavigator(navigatorConfig);

  const signInUserSession = this.props.authData.signInUserSession;
  let accessToken = null;
  if (signInUserSession) {
    accessToken = signInUserSession.accessToken.jwtToken;
    console.log(`accessToken = ${accessToken}`);
  }

  const client = new AWSAppSyncClient({
    url: appSyncConfig.graphqlEndpoint,
    region: appSyncConfig.region,
    auth: {
      type: appSyncConfig.authenticationType,
      apiKey: appSyncConfig.apiKey,
      jwtToken: accessToken
    }
  });

My question is how to update client if I get toke some times later?

good first issue question

Most helpful comment

import { Auth } from "aws-amplify"

auth: {
  type: AUTH_TYPE.AWS_IAM,
  credentials: () => Auth.currentCredentials()
}

make it a function that returns the token, this is my current setup.

All 3 comments

import { Auth } from "aws-amplify"

auth: {
  type: AUTH_TYPE.AWS_IAM,
  credentials: () => Auth.currentCredentials()
}

make it a function that returns the token, this is my current setup.

Hi @wzup, what @iskanderbroere mention is correct. jwtToken parameter can be assigned to a function that can retrieve the token. Feek free to re open the issue if you still have problems with this.

Thanks @iskanderbroere you saved my day

Was this page helpful?
0 / 5 - 0 ratings