Amplify GraphQL API: ApiAuthException{message=Failed to retrieve auth token from Cognito provider ...]

56 Views Asked by At

I am using AWS Amplify GraphQL API for an Android app. Whenever a user successfully registers on Cognito, the app creates another user object with the same userId called UserForMessaging on DynamoDB (because there are some custom attributes that Cognito doesn't store)

However, when the app tries to create UserForMessaging, it gets this error below

error creating a UserForMessaging
ApiException{message=OkHttp client failed to make a successful request., cause=ApiAuthException{message=Failed to retrieve auth token from Cognito provider., cause=ApiAuthException{message=Token is null, cause=null, recoverySuggestion=Sorry, we don't have a suggested fix for this error yet.}, recoverySuggestion=Check the application logs for details.}, recoverySuggestion=Sorry, we don't have a suggested fix for this error yet.}
at com.amplifyframework.api.aws.AppSyncGraphQLOperation.dispatchRequest(AppSyncGraphQLOperation.java:110)
at com.amplifyframework.api.aws.AppSyncGraphQLOperation.$r8$lambda$b71cZYd7jiBi4afSLLA1Swn45kU(Unknown Source:0)
at com.amplifyframework.api.aws.AppSyncGraphQLOperation$$ExternalSyntheticLambda0.run(Unknown Source:2)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:487)
at java.util.concurrent.FutureTask.run(FutureTask.java:264)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:644)
at java.lang.Thread.run(Thread.java:1012)
Caused by: ApiAuthException{message=Failed to retrieve auth token from Cognito provider., cause=ApiAuthException{message=Token is null, cause=null, recoverySuggestion=Sorry, we don't have a suggested fix for this error yet.}, recoverySuggestion=Check the application logs for details.}
at com.amplifyframework.api.aws.auth.ApiRequestDecoratorFactory.forAuthType(ApiRequestDecoratorFactory.java:127)
at com.amplifyframework.api.aws.auth.ApiRequestDecoratorFactory.fromGraphQLRequest(ApiRequestDecoratorFactory.java:100)
at com.amplifyframework.api.aws.AppSyncGraphQLOperation.dispatchRequest(AppSyncGraphQLOperation.java:94)
at com.amplifyframework.api.aws.AppSyncGraphQLOperation.$r8$lambda$b71cZYd7jiBi4afSLLA1Swn45kU(Unknown Source:0) 
at com.amplifyframework.api.aws.AppSyncGraphQLOperation$$ExternalSyntheticLambda0.run(Unknown Source:2) 
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:487) 
at java.util.concurrent.FutureTask.run(FutureTask.java:264) 
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:644) 
at java.lang.Thread.run(Thread.java:1012) 

My Auth Mode is Amazon Cognito User Pools. But I am registering a user and apparently not signed in, so I am confused with which "token" is the error referring to.

Below are some relevant codes

type UserForMessaging
@model
@auth(rules: [
    { allow: public, operations: [create, read] },
    { allow: private, operations: [update, delete] }
])
{
    userId: ID! @primaryKey
    userType: UserType!
    username: String!
    sentMessages: [Message]! @hasMany(indexName: "bySender", fields: ["userId"])
    receivedMessages: [Message]! @hasMany(indexName: "byReceiver", fields: ["userId"])
}
Amplify.Auth.signUp(username, password1, options,
      signUpResult -> runOnUiThread(() -> {
          String userId = signUpResult.getUserId();
          Log.i("RegistrationPage", "userId is " + userId);

          // create UserForMessaging
          UserType userType = registerAsTutor ? UserType.TUTOR : UserType.STUDENT;
          UserForMessaging userForMessaging = UserForMessaging.builder()
                   .userId(userId)
                   .userType(userType)
                   .username(username)
                   .build();

          Amplify.API.mutate(
              ModelMutation.create(userForMessaging),
              response -> Log.i("RegistrationPage", "created a new UserForMessaging user "),
              error -> Log.e("RegistrationPage", "error creating a UserForMessaging", error));

The reason I used API not Datastore is that I do not need the offline storage feature

0

There are 0 best solutions below