Max
Max

Reputation: 643

Separating user pools for JavaScript AWS Amplify SDK / AWS Cognito based authentication in React app

I have a React-native web application working with the authentication completely based on a user pool managed by AWS Cognito. All the authentication-related screens (login, registration, change password) are managed by AWS Amplify JavaScript SDK. There is almost no custom client code.

We face the following requirement: we need to separate the user pools between the development and the production: 2 flavors of the same application (same code) need to use different pools managed inside AWS Cognito.

What is needed to achieve such a configuiration?

Specifically, we have an auto-generated (long time ago) aws-exports.js file which looks like:

// WARNING: DO NOT EDIT. This file is automatically generated by AWS Amplify. It will be overwritten.
const awsmobile = {
    "aws_project_region": "us-east-1",
    "aws_cognito_identity_pool_id": "us-east-1:[id]",
    "aws_cognito_region": "us-east-1",
    "aws_user_pools_id": "us-east-1_[id]",
    "aws_user_pools_web_client_id": "[id]",
    "oauth": {}
};

export default awsmobile;

The ID set for "aws_user_pools_id" is indeed the ID of the pool we manage in Cognito.

The source code loading this data looks like:

import Amplify, { Auth } from 'aws-amplify';
import awsconfig from '../aws-exports';

Amplify.configure(awsconfig);
Auth.configure(awsconfig);

What needs to be done? Seems like overriding the auto-generated file is not a good idea. Should we just fix the "aws_user_pools_id" value, before calls to .configure()? Do we need to have multiple "aws_cognito_identity_pool_id" and/or "aws_user_pools_web_client_id" (under this scenario), or we can share the values between the configurations?

If there is a good documentation on "how to", I will appreciate the reference.

Thanks in advance.

Max.

Upvotes: 1

Views: 582

Answers (1)

Andrew Gillis
Andrew Gillis

Reputation: 3885

Assuming you are using the amplify cli you should use environments as shown here. The aws-exports.js file should not be committed to git. Instead it should be generated and kept up to date using amplify pull

Upvotes: 0

Related Questions