【发布时间】:2023-02-01 00:17:47
【问题描述】:
问题:我在从 amazon-cognito-identity-js 库实现 use case 27 时遇到困难,特别是在尝试修改它以使用 QR 码时。我能够从“associateSoftwareToken”接收密码,将其转换为二维码,并从验证器应用程序获取 TOTP 代码。但是,之后我很难将 TOTP 代码作为挑战答案传递给“verifySoftwareToken”。
目标:在库提供的示例中,他们使用“提示”(我已经尝试过并且有效)暂停工作流程,但我希望用户能够将 TOTP 键入页面本身的表单/输入字段而不是而不是弹出窗口。在表单上点击“提交”后,我想将 TOTP 输入提交到“verifySoftwareToken”,允许用户完成注册并被定向到主页。
我试过以这种方式分解身份验证流程,但它似乎不起作用:
const [totpInput, setTotpInput] = useState(""); // controlled by an input field underneath the QR
// Initial login event handler, which contains the main auth flow
const onSubmit = (event) => {
const authDetails = new AuthenticationDetails({
Username: email,
Password: password
});
const user = new CognitoUser({
Username: email,
Pool: UserPool
});
user.authenticateUser(authDetails, {
... success and error handling, as well as other callbacks
mfaSetup: (challengeName) => {
user.associateSoftwareToken({
onFailure: (err) => {
console.error(err);
},
associateSecretCode: (secretCode) => {
setQRCodeSecret(secretCode);
setQRCode(true); // renders the QR component on the page
// the example uses a prompt here and continues to verifySoftwareToken immediately
// after, which I'm trying to modify
}
});
}
}
// Runs when the QR form is submitted
const onSubmitTotp = (totpInput) => {
user = new CognitoUser({
Username: email,
Pool: UserPool
})
user.verifySoftwareToken(totpInput, 'cry5', {
onSuccess: (result) => {
console.log(result);
},
onFailure: (err) => {
console.log(err);
}
});
}
我遇到的错误:
- 上述方法在提交表单时抛出网络错误
- 我尝试在 useState 挂钩中声明“user”和“authDetails”,以便它们的值在渲染中保持不变,但这似乎不起作用:
...globally: const [user, setUser] = useState(); const [authDetails, setAuthDetails] = useState(); ...at the start of onSubmit function: setUser(new CognitoUser({ Username: email, Pool: UserPool })); setAuthDetails(new AuthenticationDetails({ Username: email, Password: password }));
【问题讨论】:
标签: javascript reactjs qr-code totp amazon-cognito-identity-js