You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React Native应用AWS Cognito/Amplify认证配置及Amplify定价咨询

Hey there! Let's tackle your three questions clearly and practically— I’ve worked with both Cognito and Amplify in React Native projects, so I’ll share what I know to get you sorted.

1. Standard Flow to Configure AWS Cognito in React Native

If you want to use Cognito directly without Amplify, follow this step-by-step process:

Step 1: Install Required Dependencies

Start by adding the official Cognito SDK and a network info package (to handle offline scenarios):

npm install amazon-cognito-identity-js @react-native-community/netinfo
# Or with yarn
yarn add amazon-cognito-identity-js @react-native-community/netinfo

Step 2: Set Up Cognito Resources in AWS Console

  • Head to the AWS Cognito Console, create a User Pool with your preferred settings (password policies, MFA requirements, etc.).
  • Create an App Client inside the user pool—make sure to disable "Generate client secret" (React Native is a public client, so secrets aren’t secure here).
  • Note down your UserPoolId, ClientId, and the AWS region where your pool is hosted.

Step 3: Initialize Cognito in Your App

Create a utility file (like cognitoConfig.js) to store your config and initialize the user pool:

import { CognitoUserPool } from 'amazon-cognito-identity-js';

const poolData = {
  UserPoolId: 'YOUR_USER_POOL_ID',
  ClientId: 'YOUR_APP_CLIENT_ID',
};

export const userPool = new CognitoUserPool(poolData);

Step 4: Implement Core Authentication Functions

Here are examples for common actions you’ll need:

  • User Registration:
    import { CognitoUserAttribute } from 'amazon-cognito-identity-js';
    import { userPool } from './cognitoConfig';
    
    const signUp = async (email, password) => {
      const attributeList = [
        new CognitoUserAttribute({ Name: 'email', Value: email }),
      ];
    
      return new Promise((resolve, reject) => {
        userPool.signUp(email, password, attributeList, null, (err, result) => {
          if (err) reject(err);
          else resolve(result.user);
        });
      });
    };
    
  • User Login:
    import { CognitoUser, AuthenticationDetails } from 'amazon-cognito-identity-js';
    import { userPool } from './cognitoConfig';
    
    const signIn = async (email, password) => {
      const authenticationDetails = new AuthenticationDetails({
        Username: email,
        Password: password,
      });
    
      const cognitoUser = new CognitoUser({
        Username: email,
        Pool: userPool,
      });
    
      return new Promise((resolve, reject) => {
        cognitoUser.authenticateUser(authenticationDetails, {
          onSuccess: (session) => resolve(session),
          onFailure: (err) => reject(err),
        });
      });
    };
    
  • Fetch Current User Session:
    import { userPool } from './cognitoConfig';
    
    const getCurrentSession = async () => {
      const cognitoUser = userPool.getCurrentUser();
      if (!cognitoUser) return null;
    
      return new Promise((resolve, reject) => {
        cognitoUser.getSession((err, session) => {
          if (err) reject(err);
          else resolve(session);
        });
      });
    };
    

2. Configure AWS Amplify with Only Required Features

If you opt for Amplify but want to cut out unused features, here’s how to do it cleanly:

Step 1: Install Amplify CLI and Core Packages

First, set up the CLI and install only the modules you need:

# Install Amplify CLI globally
npm install -g @aws-amplify/cli
# Install core and required modules in your app
npm install aws-amplify @aws-amplify/ui-react-native @aws-amplify/datastore

Step 2: Initialize Amplify and Add Only Needed Categories

Run amplify init in your project root, follow the prompts to connect your AWS account. Then add only the features you want:

# Add Authentication (includes user management via Cognito)
amplify add auth
# Add Storage (S3 integration)
amplify add storage
# Add DataStore (local + cloud sync via AppSync/DynamoDB)
amplify add api # Choose "GraphQL" when prompted, then select "DataStore" as the use case

Step 3: Remove Unwanted Features (If Accidentally Added)

If you added extra categories by mistake, you can remove them:

amplify remove <category-name> # e.g., amplify remove analytics

Step 4: Configure Amplify in Your App

In your root component (like App.js), initialize Amplify with only the necessary imports:

import Amplify from 'aws-amplify';
import config from './src/aws-exports';
import { Auth, Storage, DataStore } from 'aws-amplify';

// Initialize only the features you need
Amplify.configure(config);

// Now you can use Auth, Storage, DataStore directly
// Example: Auth.signIn(email, password)

Important Note

The auto-generated aws-exports.js file will only include config for the categories you added, so you won’t have redundant code for unused features.

3. Detailed AWS Amplify Pricing Rules

Amplify doesn’t charge extra fees—its cost is tied directly to the underlying AWS services it uses. Here’s a breakdown of the key costs:

Authentication (Cognito)

  • Monthly Active Users (MAU): First 5,000 MAUs per month are free. Beyond that, it’s $0.005 per MAU/month.
  • Authentication Requests:
    • First 1 million requests per month are free.
    • $0.015 per 1,000 requests after that.
  • MFA & Advanced Security:
    • SMS MFA: $0.0065 per message + carrier fees.
    • TOTP MFA: Included in MAU pricing (no extra cost).
    • Advanced Security (risk-based auth): $0.001 per MAU/month (after free tier).

Storage (S3)

  • Storage Capacity: First 5GB of standard storage is free per month. Beyond that, $0.023 per GB/month (cheaper tiers like Glacier are available for long-term storage).
  • Requests:
    • First 20,000 PUT/COPY/POST/LIST requests free; $0.005 per 1,000 after.
    • First 100,000 GET requests free; $0.0004 per 1,000 after.
  • Data Transfer: Inbound transfer is free. Outbound to the internet: first 100GB/month free, then $0.09 per GB for the next 400GB, etc.

DataStore

  • Local Storage: 100% free (uses SQLite on the device).
  • Cloud Sync (AppSync + DynamoDB):
    • AppSync: First 1 million queries/mutations/subscriptions per month are free. Beyond that, $4.00 per million queries/mutations, $2.00 per million subscriptions.
    • DynamoDB: First 25GB of storage, 2.5 million read capacity units (RCUs), and 2.5 million write capacity units (WCUs) are free per month. Beyond that, pay per RCU/WCU and storage used.

Amplify Console (If Used for Hosting)

  • Build Minutes: First 1,000 minutes per month are free. Beyond that, $0.01 per minute.
  • Bandwidth: First 15GB/month free, then $0.15 per GB.

Key Takeaway

Amplify’s cost scales with your usage of underlying services. For small to medium apps, the free tiers usually cover most needs. To stay cost-conscious, monitor usage via AWS Cost Explorer to avoid unexpected charges.


内容的提问来源于stack exchange,提问作者co_ssm

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 08:13:10