最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

AWS Cognito initiateAuth response empty when trying to authenticate a user in JavaScript - Stack Overflow

programmeradmin7浏览0评论

I'm trying to authenticate a user with AWS Cognito using the USER_PASSWORD_AUTH flow in JavaScript. However, the response from initiateAuth is always empty. Here's the code I'm using:

        var cognitoidentityserviceprovider = new AWS.CognitoIdentityServiceProvider({region: 'eu-central-1'});

        const poolData = {
            UserPoolId: "userPoolId",
            ClientId: "clientId",
        }

        let username = document.getElementById('username').value;
        let password = document.getElementById('password').value;
            var aws_params = {
            AuthFlow: "USER_PASSWORD_AUTH", 
            AuthParameters: {
                "PASSWORD": password, 
                "SECRET_HASH": "secretHash", 
                "USERNAME": username
            }, 
            ClientId: "clientId", 
        };

        response = cognitoidentityserviceprovider.initiateAuth(aws_params);

However, the response object is consistently empty, like this:

{
"request": {…},
"data": null,
"error": null,
"retryCount": 0,
"redirectCount": 0,
"httpResponse": {…},
"maxRetries": 3,
"maxRedirects": 10

}

I did the same thing but using a django view and it works: i got the token from cognito. But in js I'm stuck.

Any guidance is appreciated!

I'm trying to authenticate a user with AWS Cognito using the USER_PASSWORD_AUTH flow in JavaScript. However, the response from initiateAuth is always empty. Here's the code I'm using:

        var cognitoidentityserviceprovider = new AWS.CognitoIdentityServiceProvider({region: 'eu-central-1'});

        const poolData = {
            UserPoolId: "userPoolId",
            ClientId: "clientId",
        }

        let username = document.getElementById('username').value;
        let password = document.getElementById('password').value;
            var aws_params = {
            AuthFlow: "USER_PASSWORD_AUTH", 
            AuthParameters: {
                "PASSWORD": password, 
                "SECRET_HASH": "secretHash", 
                "USERNAME": username
            }, 
            ClientId: "clientId", 
        };

        response = cognitoidentityserviceprovider.initiateAuth(aws_params);

However, the response object is consistently empty, like this:

{
"request": {…},
"data": null,
"error": null,
"retryCount": 0,
"redirectCount": 0,
"httpResponse": {…},
"maxRetries": 3,
"maxRedirects": 10

}

I did the same thing but using a django view and it works: i got the token from cognito. But in js I'm stuck.

Any guidance is appreciated!

Share Improve this question asked Jan 17 at 21:30 CanAnyOneHelpMeCanAnyOneHelpMe 496 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Hey first of all I wanted to know which npm package you're using . I tried with package named amazon-cognito-identity-js and aws-sdk and it didn't worked for me as well. Later i found out this one "@aws-sdk/client-cognito-identity-provider .

import { CognitoIdentityProviderClient, InitiateAuthCommand } from "@aws-sdk/client-cognito-identity-provider";

async function initAuth() {
    const client = new CognitoIdentityProviderClient({
        region: "ap-southeast-2",
    });

    const poolData = {
        UserPoolId: "userPoolId",
        ClientId: "clientId",
    }

    let username = document.getElementById('username').value;
    let password = document.getElementById('password').value;
    const command = new InitiateAuthCommand({
        AuthFlow: "USER_PASSWORD_AUTH",
        AuthParameters: {
            "PASSWORD": password,
            "SECRET_HASH": "secretHash",
            "USERNAME": username
        },
        ClientId: "clientId",
    })

    const response = await client.send(command);

    console.log(JSON.stringify(response));
}

I don't have any idea why the code you shared is not working maybe some outdated apis used in that or different payload types.

发布评论

评论列表(0)

  1. 暂无评论