Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic Configuration Alternative to amplifyconfiguration.json #1662

Closed
bbrodjes opened this issue Mar 4, 2022 · 7 comments
Closed

Dynamic Configuration Alternative to amplifyconfiguration.json #1662

bbrodjes opened this issue Mar 4, 2022 · 7 comments
Assignees
Labels
core Amplify Core components question General question

Comments

@bbrodjes
Copy link

bbrodjes commented Mar 4, 2022

Hello,

I have been reading through the Amplify docs and trying things out and I see that the standard method of configuration is an "amplifyconfiguration.json" file in the app project.

We have multiple Cognito configurations on our server and we need to be able to dynamically create or update the Amplify Configuration based on API data. I am not seeing a documented way to achieve this using Amplify and I was hoping someone could help me out.

I see documentation for an in-memory object for use in the AWS Mobile SDK but we are trying to upgrade our app to use Amplify rather than the older libraries.

Any help would be greatly appreciated.

@bbrodjes bbrodjes closed this as completed Mar 4, 2022
@bbrodjes bbrodjes reopened this Mar 4, 2022
@atierian atierian added pending-triage Issue is pending triage core Amplify Core components labels Mar 4, 2022
@atierian atierian self-assigned this Mar 4, 2022
@atierian
Copy link
Member

atierian commented Mar 4, 2022

Thanks for opening this @bbrodjes.

Dynamically creating the a configuration is supported through the Amplify.configure(_ configuration: AmplifyConfiguration?) method. Here's a simple example:

let authPlugin = AWSCognitoAuthPlugin()
try Amplify.add(plugin: authPlugin)

let configuration = AmplifyConfiguration(
    auth: AuthCategoryConfiguration(
        plugins: [
            "awsCognitoAuthPlugin": [
                "CredentialsProvider": [
                    "CognitoIdentity": [
                        "Default": [
                            "PoolId": "...",
                            "Region": "..."
                        ]
                    ]
                ],
                "CognitoUserPool": [
                    "Default": [
                        "PoolId": "...",
                        "Region": "...",
                        "AppClientId": "..."
                    ]
                ]
            ]
        ]
    )
)

try Amplify.configure(configuration)

It is important to note that we currently don't support runtime configuration changes after the initial Amplify.configure() has been called. If runtime configuration changes is a feature you'd like to see in Amplify, I'd encourage you to +1 / 👍 this feature request #1584.

@atierian atierian added pending-community-response Issue is pending response from the issue requestor question General question and removed pending-triage Issue is pending triage labels Mar 4, 2022
@bbrodjes
Copy link
Author

bbrodjes commented Mar 10, 2022

Thank you for the quick reply. I integrated this into our app and we are receiving an unexpected error when fetching the session after a successful "Amplify.Auth.signIn". The only difference that I see is that I use the dynamic configuration to configure Amplify when I receive the error. The static configuration works without an issue.

Our Static Configuration:

{
    "auth": {
        "plugins": {
            "awsCognitoAuthPlugin": {
                "CognitoUserPool": {
                    "Default": {
                        "PoolId": "xxx",
                        "AppClientId": "xxx",
                        "Region": "xxx"
                    }
                }
            }
        }
    }
}   

Our Dynamic Configuration:

      let authPlugin = AWSCognitoAuthPlugin()
        try Amplify.add(plugin: authPlugin)
        
        let configuration = AmplifyConfiguration(
            auth: AuthCategoryConfiguration(
                plugins: [
                    "awsCognitoAuthPlugin": [
                        "CognitoUserPool": [
                            "Default": [
                                "PoolId": .string(userPoolId),
                                "Region": .string(region),
                                "AppClientId": .string(clientId)
                            ]
                        ],
                        "CredentialsProvider": [
                            "CognitoIdentity": [
                                "Default": [
                                    "PoolId": .string(userPoolId),
                                    "Region": .string(region)
                                ]
                            ]
                        ]
                    ]
                ]
            )
        )

       try Amplify.configure(configuration)

Error is thrown when we fetch the session:

Amplify.Auth.fetchAuthSession { result in
            do {
                let session = try result.get()

                print("Is user signed in - \(session.isSignedIn)")
                
            } catch {
                print("Fetch auth session failed with error - \(error)")

            }
}

Our calls to Amplify.Auth.signIn and signUp succeed.
The "do" reaches the "Is user signed in - true" output but then fails with an exception:

Unexpected error occurred with message: An unknown error occurred
Recovery suggestion: This should not happen. There is a possibility that there is a bug if this error persists. Please take a look at https://github.com/aws-amplify/amplify-ios/issues to see if there are any existing issues that match your scenario, and file an issue with the details of the bug if there isn't. Issue encountered at:
file: /Users/bbrodjeski/Library/Developer/Xcode/DerivedData/thrive-ios-fttjiwtgnizzvebrzumwcfyqwmls/SourcePackages/checkouts/amplify-ios/Amplify/Categories/Auth/Error/AuthError.swift
function: recoverySuggestion
line: 80
Caused by:
Error Domain=com.amazonaws.service.cognitoidentity.AWSCognitoCredentialsProviderHelper Code=0 "Obtaining an identity id in another thread failed or didn't complete within 5 seconds." UserInfo={NSLocalizedDescription=Obtaining an identity id in another thread failed or didn't complete within 5 seconds.}

I am able to switch back to the static configuration using "Amplify.configure()" and it no longer fails.
I could definitely use some assistance as it feels like we are very very close to a successful implementation.

@atierian
Copy link
Member

Thanks for these details @bbrodjes.
What you described is unexpected behavior and most likely a bug. I'll work on reproducing it and update you here with any new information.

@atierian atierian added bug Something isn't working investigating This issue is being investigated and removed pending-community-response Issue is pending response from the issue requestor labels Mar 11, 2022
@wleithrive
Copy link

Hi @atierian, any update on the dynamic config issue above?

@atierian
Copy link
Member

atierian commented Mar 28, 2022

I haven't been able to reproduce the issue you're experiencing. Would it be possible for you to provide a minimal reproducible example (with confidential info removed)?

If not, some insights or code examples showing when / how you're calling fetchAuthSession could be helpful, as well as setting logging to verbose on launch.

Amplify.Logging.logLevel = .verbose

@wleithrive
Copy link

I solved the issue. I put a print statement inside the config function while still using the ampifyconfiguration.json in the project. Then I took that output to create an exact match AmplifyConfiguration object to use with try Amplify.configure(configuration). It then works.

https://github.com/aws-amplify/amplify-ios/blob/main/Amplify/Core/Configuration/AmplifyConfiguration.swift#L114

       let configuration = try Amplify.resolve(configuration: configuration)

        print(">>>>>>> \(configuration)")`

@atierian
Copy link
Member

That's great! Thanks for the update.
I'm going to close this issue now, but please don't hesitate to open a new one if you run into any further problems.

@atierian atierian removed investigating This issue is being investigated bug Something isn't working labels Mar 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Amplify Core components question General question
Projects
None yet
Development

No branches or pull requests

3 participants