reactjs – How to obtain an authToken for API.graphql() automatically or using useAuthenticator for AWS Amplify?

I need to call an AppSync GraphQL API as part of a React SPA using `API.graphql() with Cognito User Pool authentication.

For each call, I’m fetching the user’s ID token from the current session, and using it as the authToken parameter.

How can I obtain the ID token using useAuthenticator or make the Amplify library obtain it automatically?

docs.amplify.aws/lib/graphqlapi/authz/q/platform/js/

  useEffect(() => {
    (async () => {
      const session = await Auth.currentSession();
      const token = session.getIdToken().getJwtToken();
      const allTodos = await API.graphql<GraphQLQuery<ListTodosQuery>>({
        query: quries.listTodos,
        authMode: GRAPHQL_AUTH_MODE.AMAZON_COGNITO_USER_POOLS,
        authToken: token,
      });
      console.log(allTodos);
    })();
  }, []);

Read more here: Source link