Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Amplify Cognito Auth error in Android app

I am following the Amplify docs for adding Authentication to my Android app. I'm getting an AuthException on this line:

Amplify.addPlugin(AWSCognitoAuthPlugin())

I do have a user pool created. Do I need to attach it somehow? There is some problem with the AWSMobileClient, I guess.

Here's my Application class:

class AppUtils : Application() {
    override fun onCreate() {
        super.onCreate()
        try {
            Amplify.addPlugin(AWSCognitoAuthPlugin())
            Amplify.configure(applicationContext)
            Log.d(TAG, "Initialized Amplify")
        } catch (error: AmplifyException) {
            Log.e(TAG, "Could not initialize Amplify", error)
        }
    }
}

Here's the error that shows up in logcat:

AuthException{message=Failed to instantiate AWSMobileClient, cause=java.lang.RuntimeException: Neither Cognito Identity or Cognito UserPool was used. At least one must be present to use AWSMobileClient., recoverySuggestion=See attached exception for more details}

like image 420
Yarin Shitrit Avatar asked Dec 15 '20 10:12

Yarin Shitrit


Video Answer


3 Answers

That error indicates that there was no Identity or UserPool found in your configuration file.

First make sure you've completed the following steps:

  • amplify init
  • amplify add auth
  • amplify push

Once complete, you should have an amplifyconfiguration.json and an awsconfiguration.json in your app/src/main/res/raw directory.

Your amplifyconfiguration.json should look like this:

{
    "UserAgent": "aws-amplify-cli/2.0",
    "Version": "1.0",
    "auth": {
        "plugins": {
            "awsCognitoAuthPlugin": {
                "UserAgent": "aws-amplify-cli/0.1.0",
                "Version": "0.1.0",
                "IdentityManager": {
                    "Default": {}
                },
                "CredentialsProvider": {
                    "CognitoIdentity": {
                        "Default": {
                            "PoolId": "us-east-1:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
                            "Region": "us-east-1"
                        }
                    }
                },
                "CognitoUserPool": {
                    "Default": {
                        "PoolId": "us-east-1_xxxxxxxxx",
                        "AppClientId": "xxxxxxxxxxxxxxxxxxxxxxxxxx",
                        "AppClientSecret":
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
                        "Region": "us-east-1"
                    }
                },
                "Auth": {
                    "Default": {
                        "authenticationFlowType": "USER_SRP_AUTH"
                    }
                }
            }
        }
    }
}

Your awsconfiguration.json should look like this:

{
    "UserAgent": "aws-amplify-cli/0.1.0",
    "Version": "0.1.0",
    "IdentityManager": {
        "Default": {}
    },
    "CredentialsProvider": {
        "CognitoIdentity": {
            "Default": {
                "PoolId": "us-east-1:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
                "Region": "us-east-1"
            }
        }
    },
    "CognitoUserPool": {
        "Default": {
            "PoolId": "us-east-1_xxxxxxxxx",
            "AppClientId": "xxxxxxxxxxxxxxxxxxxxxxxxxx",
            "AppClientSecret":
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
            "Region": "us-east-1"
        }
    },
    "Auth": {
        "Default": {
            "authenticationFlowType": "USER_SRP_AUTH"
        }
    }
}
like image 154
RichardMcClellan Avatar answered Oct 10 '22 08:10

RichardMcClellan


I was also facing same error for few days, you just have added basic files to the project and now you need to define user pools for Amplify to add more data to both config files. You need to run these commands it will fix this issue.

amplify init // It will make sure you have basic setup added
amplify add auth // It will add auth data and user pools to config files
amplify push // It will push all the setup to amplify cloud

Hope it will fix your issue :)

like image 34
Muhammad Khan Avatar answered Oct 10 '22 08:10

Muhammad Khan


If you have just added Cognito, run

amplify update api

to use Cognito as the auth mode.

Then run

amplify push
like image 1
live-love Avatar answered Oct 10 '22 08:10

live-love