Mat*_*les 6 javascript sdk amazon-web-services multi-factor aws-cognito
我正在尝试将用于用户身份验证的MFA添加到AWS Cognito中用于设备管理的现有解决方案(内置于Angular中).
从用户体验的角度来看,我无法弄清楚如何很好地处理这个特定的响应.它实际上感觉很破碎,如果有其他人在这里经历过痛点,那就太爱了.
参见用例23.例如实现,我的如下:
authenticate(username: string, password: string): Observable<any> {
// init cognitoUser here
return new Observable((observer) => {
cognitoUser.authenticateUser(authenticationDetails, {
onSuccess: (result: any) => {},
onFailure: (err: Error) => {},
mfaRequired: (codeDeliveryDetails: any) => {
// SMS has just been sent automatically
// and it needs to be confirmed within this scope
// The example linked requests the code via `confirm()`
// which is awful UX...and since this is a service
// probably non-compliant with best practice
// However, without this `confirm` at this point in
// time, we have no confirmationCode below
cognitoUser.sendMFACode(confirmationCode, {
onSuccess: (result) => {
observer.next(result);
observer.complete();
}, onFailure: (err: Error) => {
observer.error(err);
observer.complete();
}
});
}
});
});
}
Run Code Online (Sandbox Code Playgroud)
预期:
sendMFACode手动触发该功能(可能通过某种有限的会话?)发行/ S:
sendMfaCode多次命中该功能,导致发送多个SMS代码.有没有人有任何运气退出这一流程?
虽然我确信非常有才华的人在amazon-cognito-identity-js API 上工作过,但它只是设计得很糟糕。这就是为什么它被贬低了。我个人的建议是迁移到Amplify,这让我不那么生气。
使用Amplify,您可以做到这些。???
import Amplify from 'aws-amplify'
import Auth from '@aws-amplify/auth'
let mfaRequired = false
Amplify.configure({
Auth: {
userPoolWebClientId: '',
userPoolId: ''
}
})
const logUserIn = (user) => {
// Go forth and be happy
}
// Run me on your login form's submit event
const login = async (username, password) => {
const user = await Auth.signIn(username, password)
if (user.challengeName === 'SMS_MFA') {
// Change UI to show MFA Code input
mfaRequired = true
return
}
return logUserIn(user)
}
// Run me when the user submits theire MFA code
const senfMfaCode = async (mfaCode) => {
const user = await Auth.confirmSignIn(mfaCode)
return logUserIn(user)
}
Run Code Online (Sandbox Code Playgroud)
但是,如果出于某种可悲的原因您需要继续使用amazon-cognito-identity-js,请不要担心。我接到你了。
只需将cognitoUser对象存储在回调之外。该文档有点误导,因为它只显示了自包含的示例,但没有理由在需要 MFA 时通知您的 UI,然后再调用cognitoUser.sendMFACode()。
请记住,文档显示了传递thistosendMFACode()范围(这很糟糕),但您可以将回调声明为变量并在您的authenticateUser()和sendMFACode()函数(或您喜欢的任意数量的函数)之间共享它。
import { CognitoUserPool, AuthenticationDetails, CognitoUser } from 'amazon-cognito-identity-js'
export let mfaRequired = false
export let cognitoUser = null
export const cognitoCallbacks = {
mfaRequired () {
// Implement you functionality to show UI for MFA form
mfaRequired = true
},
onSuccess (response) {
// Dance for joy the code gods be glorious.
},
onFailure () {
// Cry.
}
}
export const logUserIn = payload => {
cognitoUser = new CognitoUser({
Username: 'Matt Damon',
Pool: new CognitoUserPool({
UserPoolId: '',
ClientId: ''
})
})
return cognitoUser.authenticateUser(new AuthenticationDetails(payload), cognitoCallbacks)
}
export const sendMfaCode = MFACode => {
cognitoUser.sendMFACode(MFACode, cognitoCallbacks)
}
Run Code Online (Sandbox Code Playgroud)
这是一个超级基本的实现,最重要的是你可以,
mfaRequired外部模块中的函数即可执行任何您想要的操作。希望有帮助!
| 归档时间: |
|
| 查看次数: |
2127 次 |
| 最近记录: |