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 badges1 Answer
Reset to default 0Hey 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.