aws-sdk-android icon indicating copy to clipboard operation
aws-sdk-android copied to clipboard

Any way to initialize AWSMobileClient with dynamic credentials or update AWSConfiguration afterwards?

Open cheeyi opened this issue 6 years ago • 3 comments

State your question I'm writing an app that will obtain identity pool information from the user, specifically the pool ID, and want to use that with AWS IoT. But as it stands I need to initialize AWSMobileClient with the awsconfiguration.json, which is fine if I want a default fallback, but is there a way to change the AWSMobileClient's AWSConfiguration object after the original initialize() call? The iOS SDK was able to do this by registering the manager-type singletons with a new config object every time I need to update the credentials (e.g. when user signs out and uses another set of credentials altogether)

Which AWS Services are you utilizing?

  • AWS Cognito
  • AWS IoT

Provide code snippets (if applicable)

// During init, AWSMobileClient is configured with awsconfiguration.json's fields
AWSMobileClient.getInstance().initialize(mApplicationContext, new Callback<UserStateDetails>() {
    @Override
    public void onResult(UserStateDetails result) {
        Log.w(TAG, "AWSMobileClient.getInstance().initialize produced an onResult callback. " + result.getUserState().name());
    }

    @Override
    public void onError(Exception e) {
        Log.e(TAG, "AWSMobileClient.getInstance().initialize produced an onError callback.");
        e.printStackTrace();
    }
});

// After this if I want to update AWSMobileClient's AWSConfiguration field how do I do that? Or what other classes should I use, if this is an API misuse?

Environment(please complete the following information):

  • SDK Version: 2.9.1

Device Information (please complete the following information):

  • Device: Pixel XL
  • Android Version: Pie 9.0
  • Specific to simulators:

If you need help with understanding how to implement something in particular then we suggest that you first look into our developer guide. You can also simplify your process of creating an application, as well as the associated backend setup by using the Amplify CLI.

cheeyi avatar Dec 19 '18 11:12 cheeyi

Hi @cheeyi ,

The AWSMobileClient acts as a singleton that does not change it's configuration during run-time. You may need to fallback to the CognitoCachingCredentialsProvider for your use-case. I will discuss with the team about this issue.

minbi avatar Dec 25 '18 22:12 minbi

@minbi Thanks for getting back to me. I can look into using CognitoCachingCredentialsProvider but are its public APIs going to be deprecated in future versions of this library?

Also, having AWSMobileClient always read credentials from a resource JSON file assumes the credentials are already known during compile time which is something my app does not know until I receive user input. I'd be open into looking at other options aside from CognitoCachingCredentialsProvider if you have any other suggestions as well.

cheeyi avatar Dec 26 '18 02:12 cheeyi

We have a need for the ability to swap between various pools at runtime, too. We use a separate identity pool for each backend environment and we also provide internal users of our mobile app (that talks to that backend) the ability to switch between those environments. This single AWSMobileClient.getInstance() function doesn't allow for that. We wouldn't require an instance of AWSMobileClient to be able to hot swap between pools, but we would need the ability to configure an instance to use a pool using some means other than the awsconfiguration.json.

Like @cheeyi, I'll gladly use the CognitoCachingCredentialsProvider in the meantime, but it would be nice to know the functionality won't be going away and will be formally supported.

mtrewartha avatar Feb 11 '19 23:02 mtrewartha