amplify-swift
amplify-swift copied to clipboard
Configuration switching
Is your feature request related to a problem? Please describe.
An app which is used in multiple regions will need to switch configurations to use a different configuration. When a user logs out and then logs into a different region it is necessary to change the configuration. There is no support currently do this without restarting the app.
Describe the solution you'd like
Support an API to define multiple configurations and switch between regions.
Describe alternatives you've considered
None
Is the feature request related to any of the existing Amplify categories?
Auth
Additional context
No response
#527 Related issue
https://github.com/aws-amplify/amplify-android/discussions/215 Related issue on Android
There are multiple scenarios to cover. These are listed below.
- Loading a Amplify configuration so it is specific to a region
- Changing the configuration at build time
- Changing the configuration after the app has been launched and configured
Scenario 1
By default when Amplify is configured it will load amplifyconfiguration.json from the main bundle of the app. It is possible to use an alternative .json file by loading it and creating an instance of AmplifyConfiguration with the URL for that file. The selection can be based on whatever logic is appropriate. One use case is to use a specific configuration for a region. Below is some sample code which could be used to configure Amplify by region. It requires placing each .json file into the app bundle.
enum Failure: Error {
case configurationFileNotFound
}
init() {
do {
try configureAmplify(region: "EU")
} catch {
fatalError("Failed to configure Amplify: \(error)")
}
}
func configureAmplify(region: String) throws {
guard let configPath = Bundle.main.path(forResource: "amplifyconfiguration-\(region)",
ofType: "json") else {
throw Failure.configurationFileNotFound
}
let configURL = URL(fileURLWithPath: configPath)
let configuration = try AmplifyConfiguration(configurationFile: configURL)
// TODO: add plugins
try Amplify.configure(configuration)
}
Scenario 2
The Amplify CLI supports multiple environments such has dev, qa and prod. You can switch between these environments which will change the contents of amplifyconfiguration.json which is a resource file included in the app bundle when it is built. When the environment is changed it is best to also update the App ID which will install the app as a separate app from another App ID since it will create keychain items and other resources specific to that Amplify environment. Using the CLI to change the environment and set the App ID will support scenario 2 above.
Scenario 3
Dynamically changing the configuration after the app has already been configured is not supported. This is due to operations which are still in process. There is no support tracking all operations which could cancel each of them and wait until they are completed before switching over to the other configuration. This issue covers this scenario as a feature request.
Anyone has any updates, please?
@sonic555gr The team is still trying to prioritize the feature internally. We will post an update on the issue as soon as we have more information.
Thank you. This feature is needed for our business model. As we have 1 app with multiple potential pools with different accounts for every physical user. At the moment, after the user logs out from one pool and tries to login into a different pool, we force shut the app and ask them to relaunch which feels wrong as UX goes. This feature will go a long way to improve our app.
At this moment, Amplify does not offer any features/workarounds that can support this use case. Please keep watching this issue for updates.