简体   繁体   中英

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}

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"
        }
    }
}

If you have just added Cognito, run

amplify update api

to use Cognito as the auth mode.

Then run

amplify push

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:)

I had the same problem.The problem is when I was configuring in the terminal the sign in method.

There are 3 supported sign in methods:

  1. signIn (username - password sign in via your own native UI)
  2. signInWithWebUi (username - password sign in through a hosted UI webview)
  3. signInWithSocialWebUi - (sign in with Google, Facebook, etc)

For (1), you need to choose "Default Configuration". Once complete, run amplify push. At this point, you should expect NOT to have an OAuth object in your amplifyconfiguration.json. For (2) or (3), you need to choose "Default configuration with Social Provider (Federation)". Admittedly, the wording is probably a little confusing, since (2) is not a social provider. At this point, you SHOULD expect to have an OAuth object in your amplifyconfiguration.json

To update this use:

sudo amplify update auth

Choose the correct sign in method and

sudo amplify push

If you get this error when using the sign in method 2: Can't fill in URI in "amplify add auth"

Use this uris: ( https://www.amazon.com/ or http://localhost:3000/)

Or

Type gettingstarted://

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM