可以单独在React应用中单独使用Amplify Auth吗?

时间:2019-07-07 19:22:00

标签: reactjs amazon-web-services aws-amplify amplifyjs

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

现在,由于其界面简洁,我正尝试用aws amplify auth替换该库。但是我不想经历设置过程(放大init和所有内容),我想像使用amazon-cognito-identity-js一样使用它。

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

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

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);
    }

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

enter image description here 谁能告诉我我正在尝试的可能吗?您是否认为我只能在客户端中单独使用Auth的{​​{1}},而没有任何云或仅用于用户注册和登录用户池的任何内容?

2 个答案:

答案 0 :(得分:1)

好的,我已经弄清楚了为什么会发生错误。我写下来作为答案,以便偶然发现该问题的人可以获得答案-

似乎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.
    }
});

答案 1 :(得分:1)

That is an issue for this.

您已正确找到答案,AWS Amplify正在添加Analytics。但是,建议的解决方法是使用modular imports:

,而不是像您一样使用禁用的Analytics配置它
import Amplify from '@aws-amplify/core';
import Auth from '@aws-amplify/auth';

如果愿意

import Amplify from 'aws-amplify';

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