【问题标题】:Is it possible to use only Amplify Auth in a react app in isolation?是否可以单独在反应应用程序中使用 Amplify Auth?
【发布时间】:2019-11-17 09:45:17
【问题描述】:

我在这方面有点新意。我有一个基于反应的网络应用程序,我们一直在使用 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);
    }

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

谁能告诉我是否有可能我正在尝试什么?你认为我只能在客户端单独使用aws amplifyAuth 没有任何云或任何东西,只是用户注册和登录用户池?

【问题讨论】:

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


    【解决方案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,
        }
    });
    

    【讨论】:

      【解决方案2】:

      That is an issue for this.

      您猜对了,AWS Amplify 正在添加分析功能。但建议的解决方法是使用modular imports:

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

      如果你这样做

      import Amplify from 'aws-amplify';
      

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

      【讨论】:

      • 注意你不需要导入''Amplify',你可以简单地导入'Auth'然后使用'Auth.configure({...})'
      猜你喜欢
      • 2023-03-15
      • 2021-11-03
      • 1970-01-01
      • 1970-01-01
      • 2012-04-12
      • 2020-06-10
      • 2021-09-17
      • 2021-02-24
      • 1970-01-01
      相关资源
      最近更新 更多