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}
That error indicates that there was no Identity or UserPool found in your configuration file.
First make sure you've completed the following steps:
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"
}
}
}
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 :)
If you have just added Cognito, run
amplify update api
to use Cognito as the auth mode.
Then run
amplify push
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With