reactjs - AWS Cognito SMS 2FA:用户的代码或身份验证状态无效
问题描述
为了重现,我
- 输入已确认用户的电子邮件地址,然后
- 确认用户的密码
- 接受错误:
Missing required parameter SMS_MFA_CODE
因为this.state.code
当它传递给时是一个空字符串,是传递cognitoUser.sendMFACode(context.state.code, this)
的context
值this
throughsignIn()
- 输入通过 SMS 发送的代码
- 调用相同的
cognitoUser.authenticateUser()
函数,这次this.state.code
填充的不仅仅是一个空字符串。
在与这个小伙子完全不同的情况下,我遇到了同样的错误: AWS Cognito's SMS Multi Factor Authentication returns invalid code or auth state
我在以下 npm 使用指南中遵循案例 23 中的格式: https ://www.npmjs.com/package/amazon-cognito-identity-js (来自) https://docs.aws.amazon.com/ cognito/latest/developerguide/using-amazon-cognito-identity-user-pools-javascript-example-authenticating-admin-created-user.html?shortFooter=true
import React, { Component } from 'react'
import { BrowserRouter as Router, Route, Link, Redirect } from "react-router-dom";
import * as AWS from 'aws-sdk/global';
import { AuthenticationDetails, CognitoUserPool, CognitoUserAttribute, CognitoUser } from 'amazon-cognito-identity-js';
import PropTypes from 'prop-types'
import '../../stylesheets/Authentication.css'
export default class CognitoIdentitySignIn extends Component {
constructor(props) {
super(props);
this.state = {
password: "",
email: "",
code: "",
submissionAnnouncement: "",
readyForCode: false
};
this.handleChangeEmail = this.handleChangeEmail.bind(this);
this.handleChangePassword = this.handleChangePassword.bind(this);
this.handleChangeCode = this.handleChangeCode.bind(this);
this.submitCredentials = this.submitCredentials.bind(this);
}
handleChangeEmail(event) {
this.setState({email: event.target.value});
}
handleChangePassword(event) {
this.setState({password: event.target.value});
}
handleChangeCode(event) {
this.setState({code: event.target.value});
}
submitCredentials(event) {
this.signIn(this, this.state.email, this.state.password);
}
signIn(context, email, password) {
var authenticationData = {
Username : email,
Password : password
};
var authenticationDetails = new AuthenticationDetails(authenticationData);
var poolData = {
UserPoolId: 'us-west-2_xxxxxxxx',
ClientId: 'XXXXXXXXXXXXXXXXXXXXXX',
};
var userPool = new CognitoUserPool(poolData);
var userData = {
Username : email,
Pool : userPool
};
var cognitoUser = new CognitoUser(userData);
console.log(authenticationDetails);
console.log(cognitoUser);
cognitoUser.authenticateUser(authenticationDetails, {
onSuccess: function (session) {
var accessToken = session.getAccessToken().getJwtToken();
AWS.config.region = 'us-west-2';
// Change the key below according to the specific region your user pool is in.
var Logins = {}
Logins[`cognito-idp.${AWS.config.region}.amazonaws.com/${poolData.UserPoolId}`] = accessToken;
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
IdentityPoolId : 'us-west-2:XXXXXXX-XXXX-XXXXXXXX',
Logins : Logins
});
//refreshes credentials using AWS.CognitoIdentity.getCredentialsForIdentity()
AWS.config.credentials.refresh((error) => {
if (error) {
alert('onSuccess:\n' + JSON.stringify(error));
context.setState({
submissionAnnouncement: error.message,
readyForCode: false
})
console.error(error);
} else {
// Instantiate aws sdk service objects now that the credentials have been updated.
// example: var s3 = new AWS.S3();
context.setState({
submissionAnnouncement: "SUCCESS",
readyForCode: true
});
console.log(session);
var cognitoUser = session.user;
}
});
},
onFailure: function(err) {
alert('onFailure:\n' + JSON.stringify(err));
if (err.message === "Missing required parameter SMS_MFA_CODE") {
context.setState({
submissionAnnouncement: err.message,
readyForCode: true
})
} else {
context.setState({
submissionAnnouncement: err.message
})
}
},
mfaRequired: function(codeDeliveryDetails) {
// MFA is required to complete user authentication.
// Get the code from user and call
console.log(codeDeliveryDetails)
cognitoUser.sendMFACode(context.state.code, this)
}
// ,
// newPasswordRequired: function(userAttributes, requiredAttributes) {
// not actually doing anything with this
// }
});
}
render() {
return (
<div className='SignIn'>
<div className='sign-in-input-box'>
<label>
<p>Email</p>
<input className='sign-in-email-input' value={this.state.email} onChange={this.handleChangeEmail} type='text' />
</label>
</div>
<div className='sign-in-input-box'>
<label>
<p>Password</p>
<input className='sign-in-password-input' value={this.state.password} onChange={this.handleChangePassword} type='text' />
</label>
</div>
<div className="click-text" onClick={this.submitCredentials}> SIGN IN </div>
{(this.state.submissionAnnouncement === "" ? null : <div><span>{this.state.submissionAnnouncement}</span></div>)}
{(this.state.readyForCode) ?
<div className='sign-in-input-box'>
<label>
<p>Password</p>
<input className='sign-in-code-input' value={this.state.code} onChange={this.handleChangeCode} type='text' />
</label>
</div>:
null}
</div>
)
}
}
CognitoIdentitySignIn.propTypes = {
}
解决方案
推荐阅读
- c++ - 使用 dlsym() 加载具有多态性的 C++ 类
- c# - 实体框架到 LINQ 日期搜索
- centos7 - qemu-kvm 崩溃“来自 qemu 监视器的文件结束”
- sql - 如何在 SQL Server 中提取字符串的特定部分?
- arrays - API 在检索所有记录时返回对象数组,但在过滤时仅返回单个对象
- python-3.x - FB Prophet 检测到不正确的变化点
- scala - 如何获取 Kafka 消费者(Alpakka)返回的 Map [String, String]?
- c# - 防止虚函数调用的泛型?
- apache-kafka - 是否可以仅使用消息(即使用 kafka)触发 oozie 工作流程
- sentiment-analysis - 情绪分析中的人工智能偏见