我正在尝试使用 JavaScript 中的 USER_PASSWORD_AUTH 流通过 AWS Cognito 对用户进行身份验证。但是,initiateAuth 的响应始终为空。这是我正在使用的代码:
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);
但是,响应对象始终为空,如下所示:
{
"request": {…},
"data": null,
"error": null,
"retryCount": 0,
"redirectCount": 0,
"httpResponse": {…},
"maxRetries": 3,
"maxRedirects": 10
}
我做了同样的事情,但是使用了 django 视图并且它有效:我从 cognito 获得了令牌。但在 js 中我被困住了。
感谢任何指导!
嘿,首先我想知道你正在使用哪个 npm 包。我尝试使用名为
amazon-cognito-identity-js
和 aws-sdk
的软件包,但它对我来说也不起作用。后来我发现了这个"@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));
}
我不知道为什么您共享的代码不起作用,可能是在该代码或不同的有效负载类型中使用了一些过时的 API。