Amazon web services 无法设置AWS amplify MFA-Auth.confirmSignIn函数存在问题
我在向我的站点添加MFA时遇到问题,该站点使用AWS Amplify管理身份验证 我遵循了这个示例,创建了下面的代码来处理启用了MFA的用户和未启用MFA的用户Amazon web services 无法设置AWS amplify MFA-Auth.confirmSignIn函数存在问题,amazon-web-services,amazon-cognito,aws-amplify,amplifyjs,multi-factor-authentication,Amazon Web Services,Amazon Cognito,Aws Amplify,Amplifyjs,Multi Factor Authentication,我在向我的站点添加MFA时遇到问题,该站点使用AWS Amplify管理身份验证 我遵循了这个示例,创建了下面的代码来处理启用了MFA的用户和未启用MFA的用户 const Signin = props => { const { classes, onStateChange, history } = props; const [inputs, setInputs] = useState({ username: '', password: '', }); const [cur
const Signin = props => {
const { classes, onStateChange, history } = props;
const [inputs, setInputs] = useState({
username: '',
password: '',
});
const [currentStep, setCurrentStep] = useState(1)
const [userObject, setUserObject] = useState({})
const [authCode, setauthCode] = useState({code: ''})
const onSignIn = async (e) => {
const username = inputs.username.toLowerCase()
await Auth.signIn({
username, // Required, the username
password: inputs.password, // Optional, the password
}).then(user => {
if (user.preferredMFA === 'SOFTWARE_TOKEN_MFA'){
setUserObject(user)
setCurrentStep(2)}
else{
onStateChange('signedIn', {})
history.push('/dashboard');
}
})
.catch(err => {
showAlert(err.message)
if (err.code === 'PasswordResetRequiredException') {
onStateChange('forgotPassword')
history.push('/forgotpassword');
}
})
}
const MfaSignIn = async (e) => {
const user=userObject
await Auth.confirmSignIn(
user,
authCode.code,
user.preferredMFA
).then(user => {
onStateChange('signedIn', {})
history.push('/dashboard');
})
.catch(err => {
showAlert(err.message)
if (err.code === 'PasswordResetRequiredException') {
onStateChange('forgotPassword')
history.push('/forgotpassword');
}
})
如果未启用MFA,登录页面将调用onSignIn函数,然后登录。如果启用MFA,则从signin函数返回的用户对象将返回preferredMFA密钥的值“SOFTWARE_TOKEN_MFA”,并将加载第二个页面,允许用户输入MFA代码,该代码由MfaSignIn函数处理
没有MFA可以正常工作,但是当尝试按原样使用MfaSignIn函数时,我得到“缺少必需的参数会话”
onSignIn返回的用户对象的会话密钥的值为null,文档中没有提到需要添加它。我试着把这个功能调整到
const MfaSignIn = async (e) => {
e.preventDefault(authCode.code);
const session= Auth.currentSession().then(data=> {return data})
const token = await session.then(data=>{return data.getAccessToken()})
const user=userObject
user.Session=token.jwtToken
await Auth.confirmSignIn(
user,
authCode.code,
user.preferredMFA
).then(user => {
onStateChange('signedIn', {})
history.push('/dashboard');
})
.catch(err => {
showAlert(err.message)
if (err.code === 'PasswordResetRequiredException') {
onStateChange('forgotPassword')
history.push('/upgrade');
}
})
}
其中,我调用Auth.currentSession方法,并将其中的数据添加到用户对象中的会话值。添加整个对象将返回错误-
“在未预期的位置找到结构或映射的开始。”
-似乎需要字符串而不是贴图对象
我尝试添加currentSession对象中的三个JWT令牌字符串中的每一个,以及上面示例中的getAccessToken结果。全部返回错误
“提供的会话无效”
我不知道我需要给Auth.confirmsign什么才能让用户登录-我似乎按照文档做的每件事都是正确的
有什么想法吗