【问题标题】:AWS Cognito: Create User IssueAWS Cognito:创建用户问题
【发布时间】:2022-01-27 20:05:25
【问题描述】:

我们的要求是在 AWS Cognito 中创建一个用户,使用手机和密码确认,然后在注册过程中更新用户属性,如姓名、城市、电子邮件等。要实现这一点,首先调用“Auth.confirmSignUp”以获取用于手机号码确认的 OTP,然后调用“Auth.signIn”以通过“Auth.updateUserAttributes”更新属性,但问题是“Auth.signIn”失败并返回状态代码为 400,响应如下。

回复:

{"__type":"InvalidParameterException","message":"未为用户池配置自定义身份验证 lambda 触发器。"}

请求:

{"AuthFlow":"CUSTOM_AUTH","ClientId":"XXXXXXX","AuthParameters":{"USERNAME":"447XXXXXXXX"},"ClientMetadata":{}}

但是,如果我在登录页面上调用 Auth.signIn,它工作正常,但观察到 "AuthFlow":"CUSTOM_AUTH" 更改为 "AuthFlow":"USER_SRP_AUTH"。

无法理解在整个过程中缺少什么或为解决问题而需要进行的任何其他配置更改。

我们在 React 中使用 aws-amplify 库作为前端。代码 sn-p 如下。

awsUtils.js ==========

export const signUp = async (userName, password, attributes = {}) => {
    try {
        const response = await Auth.signUp({
            username: userName,
            password,
            attributes: {
                phone_number: userName,
                'custom:role': USER_ROLES.CUSTOMER,
                ...attributes
            }
        });
        return { success: true, response };
    } catch (error) {
        return { success: false, error };
    }
};

export const confirmSignUp = async (userName, otp) => {
    try {
        const response = await Auth.confirmSignUp(userName, otp);
        return { success: true, response };
    } catch (error) {
        return { success: false, error };
    }
};

export const signIn = async (userName, password) => {
    try {
        const response = await Auth.signIn(userName, password);
        return { success: true, response };
    } catch (error) {
        return { success: false, error };
    }
};

在 react 组件第一步中,我们只显示电话号码和密码:===========

const { success } = await signUp(userName, password);
if(success){ //if successfull then show OPT field on client side.
    const { success } = await confirmSignUp(userName, otp);
    if( success ){ //If OPT is confirmed on congnito then a call is made to singin.
         const { success } = await signIn(userName, password); // But breaking here ...
    }
}

参考了https://docs.amplify.aws/lib/auth/emailpassword/q/platform/js/#sign-in

AWS 配置:

第一次在组件挂载(load)上我们调用下面的aws配置函数。

export const initializeSDK = async () => {
    const {
        regin,
        redirectSignIn,
        redirectSignOut,
        domain,
        identityPoolId,
        poolId,
        webClientId,
        scope
    } = await getEnvConfig();

    const oauth = {
        domain,
        scope: scope?.split(','),
        redirectSignIn,
        redirectSignOut,
        responseType: 'token'
    };
    Amplify.configure({
        oauth: oauth,
        aws_project_region: regin,
        aws_cognito_identity_pool_id: identityPoolId,
        aws_user_pools_id: poolId,
        aws_user_pools_web_client_id: webClientId,
        domain: domain,
        scope: scope,
        redirectSignIn: redirectSignIn,
        redirectSignOut: redirectSignOut,
        responseType: 'CODE',
        AdvancedSecurityDataCollectionFlag: 'true',
        aws_cognito_region: regin,
        socialResponseType: 'token'
    });
};

【问题讨论】:

    标签: amazon-web-services amazon-cognito


    【解决方案1】:

    问题很明显。您的请求包含 AuthFlow 的 CUSTOM_AUTH。自定义身份验证需要 lambda。如果您想要常规的用户名密码身份验证,请使用 USER_PASSWORD_AUTH

    【讨论】:

    • 感谢@Ninad 的投入。但只是想检查是否有任何明确的方法可以将 'AuthFlow:"CUSTOM_AUTH"' 转换为 'AuthFlow:"USER_SRP_AUTH"'。
    • @Ashu 你在哪里使用 Auth.signIn?你有自己的前端设置吗?您使用什么语言/库?您可以为此分享代码 sn-p 吗?
    • 已分享以上信息和代码供您参考。
    • @Ashu 你有 awsconfiguration.json 文件吗?
    • 嗨 @Ninad,已更新 AWS 配置供您参考。
    猜你喜欢
    • 2021-09-04
    • 2019-09-04
    • 2017-12-03
    • 2020-09-11
    • 2017-04-23
    • 1970-01-01
    • 2019-07-31
    • 2018-07-28
    • 2021-08-13
    相关资源
    最近更新 更多