This code is intended to enroll a recovery code after successfully enrolling webauthn-platform
Current Behavior
Auth0 trigger > post login: User can skip webauth enrollment by clicking “No thanks” and the next subsequent action “api.authentication.enrollWith({type: ‘recovery-code’})” is called
Expected Behavior
Auth0 trigger > post login: User can skip webauth enrollment by clicking “No thanks” and the next subsequent action should not be called
My goal is to ensure that api.authentication.enrollWith({type: 'recovery-code'}) is not called if the user hasn’t successfully registered for WebAuthn
async function main(event, api) {
try {
// First attempt WebAuthn enrollment
await api.authentication.enrollWith({type: 'webauthn-platform'});
// Check if webauthn-platform is now in enrolled factors
const hasWebAuthn = event.user.enrolledFactors?.some(
factor => factor.type === 'webauthn-platform'
);
// Only proceed with recovery code if WebAuthn was successfully enrolled
if (hasWebAuthn) {
return api.authentication.enrollWith({type: 'recovery-code'});
}
} catch (error) {
console.log('Enrollment error:', error);
return;
}
}
However, there’s one potential limitation to consider: The event.user.enrolledFactors might not be immediately updated after the WebAuthn enrollment in the same action execution. If this is the case, we might need to handle this differently, perhaps by: