我有一个托管在 AWS S3 上的 Web 应用程序,它允许用户使用 AWS Cognito 进行身份验证。
使用以下命令在 AWS CDK (1.70.0) 中配置用户池:
const userPool = new cognito.UserPool(this, "user-pool", {
enableSmsRole: false,
mfa: cognito.Mfa.OFF,
signInAliases: {
email: true
},
standardAttributes: {
email: { mutable: true, required: true }
},
userPoolName: "user-pool"
});
const client = userPool.addClient("user-pool-client", {
preventUserExistenceErrors: true,
authFlows: {
userSrp: true
},
oAuth: {
flows: {
implicitCodeGrant: true,
},
scopes: [cognito.OAuthScope.OPENID, cognito.OAuthScope.EMAIL]
}
});
AWS Amplify 配置如下:
Amplify.configure({
Auth: {
authenticationFlowType: 'USER_SRP_AUTH',
mandatorySignIn: true,
region: "eu-west-1",
userPoolId: "XXXXXXXXXXXXX",
userPoolWebClientId: "XXXXXXXXXXXXX",
oauth: {
domain: "somedomain.auth.eu-west-1.amazoncognito.com",
redirectSignIn: ["https://abc123.cloudfront.net", "localhost:8080"],
redirectSignOut: "",
responseType: "token",
scope: ["email"]
},
},
});
但是,当我尝试使用
Auth.signIn(email, password);
登录时,发送到 cognito-idp 的请求负载是
{
"AuthFlow":"CUSTOM_AUTH",
"ClientId":"XXXXXXXXXXX",
"AuthParameters":{"USERNAME":"[email protected]"},
"ClientMetadata":{}
}
我收到错误
CUSTOM_AUTH is not enabled for the client
。
我是否缺少某些配置?我很困惑为什么 Amplify 尝试使用 CUSTOM_AUTH,即使我在配置中明确声明了 USER_SRP_AUTH。
解决了。
答案很简单,使用
Auth.federatedSignIn()
而不是 Auth.signIn()
。没有特别明确的错误消息或文档。如果其他人有同样的问题,请将其留在这里。
对我来说,发生这种情况是因为我忘记设置密码。
https://github.com/aws-amplify/amplify-js/issues/4915#issuecomment-599812751