是否可以在隔离的 React 应用程序中仅使用 Amplify Auth?

Is it possible to use only Amplify Auth in a react app in isolation?

我对此有点陌生。我有一个基于 React 的 Web 应用程序,我们一直在使用 AWS Cognito 进行身份验证。我正在使用 amazon-cognito-identity-js 在用户池中注册用户并进行登录。

现在我正尝试用 aws amplify auth 替换那个库,因为它的界面很干净。但我不想完成设置过程(放大初始化和所有内容),我想像使用 amazon-cognito-identity-js 一样使用它。

这是我到目前为止所做的,

我已经在我的 app.js 文件中配置了 Amplify Auth -

import Amplify from 'aws-amplify';

Amplify.configure({
    Auth: {

        // REQUIRED only for Federated Authentication - Amazon Cognito Identity Pool ID
        identityPoolId: 'my id pool',

        // REQUIRED - Amazon Cognito Region
        region: 'my-region',

        // OPTIONAL - Amazon Cognito User Pool ID
        userPoolId: 'my-userpool',

        // OPTIONAL - Amazon Cognito Web Client ID (26-char alphanumeric string)
        userPoolWebClientId: 'my app client',

        // OPTIONAL - Enforce user authentication prior to accessing AWS resources or not
        mandatorySignIn: true,

        // OPTIONAL - Manually set the authentication flow type. Default is 'USER_SRP_AUTH'
        authenticationFlowType: 'USER_SRP_AUTH'
    }
});

这是我在我的 Registration 组件中完成的注册操作 -

const { username, password, email, name } = this.state;
    try {
        const result = await Auth.signUp({
            username,
            password,
            attributes: {
                'name': name,
                'email': email,
                'phone_number': '',
            },
        });

        this.setState({showVerificationCode: true});
    } catch(e) {

        console.log(e);
    }

现在,当我尝试在我的用户池中注册一个用户时,该用户已创建并且验证邮件也已发送。但是在客户端我收到了这个错误 -

谁能告诉我我正在尝试的是否可能?你认为我可以在客户端仅使用 Authaws amplify 隔离,而无需任何云或任何用户注册和登录用户池吗?

好的,我已经弄清楚错误发生的原因了。我写下来作为答案,以便偶然发现这个的人可以得到答案 -

似乎 aws amplify 默认使用 Analytic 服务并尝试记录 'auth' 事件。所以我需要在配置中禁用它 -

Amplify.configure({
    Auth: {

        // REQUIRED only for Federated Authentication - Amazon Cognito Identity Pool ID
        identityPoolId: 'my id pool',

        // REQUIRED - Amazon Cognito Region
        region: 'my-region',

        // OPTIONAL - Amazon Cognito User Pool ID
        userPoolId: 'my-userpool',

        // OPTIONAL - Amazon Cognito Web Client ID (26-char alphanumeric string)
        userPoolWebClientId: 'my app client',

        // OPTIONAL - Enforce user authentication prior to accessing AWS resources or not
        mandatorySignIn: true,

        // OPTIONAL - Manually set the authentication flow type. Default is 'USER_SRP_AUTH'
        authenticationFlowType: 'USER_SRP_AUTH'
    },

    Analytics: {
        disabled: true,
    }
});

That is an issue for this.

您猜对了,AWS Amplify 正在添加分析。但是,与其像您那样使用禁用的 Analytics 配置它,建议的修复方法是使用 modular imports:

import Amplify from '@aws-amplify/core';
import Auth from '@aws-amplify/auth';

如果你这样做

import Amplify from 'aws-amplify';

它会自动加载 Auth,这会导致错误。