-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmfa-webauthn-challenge.js
More file actions
59 lines (51 loc) · 2 KB
/
mfa-webauthn-challenge.js
File metadata and controls
59 lines (51 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/**
* MFA WebAuthn challenge page — prompts the user to verify with their passkey
* after initial password authentication when MFA is enabled.
*/
import { showMessage } from '/js/shared.js';
import { isWebAuthnSupported } from '/js/user/webauthn-utils.js';
import { authenticateWithPasskey } from '/js/user/webauthn-authenticate.js';
const BUTTON_LABEL = 'Verify with Passkey';
const BUTTON_ICON_CLASS = 'bi bi-key me-2';
function setButtonReady(btn) {
btn.textContent = '';
const icon = document.createElement('i');
icon.className = BUTTON_ICON_CLASS;
btn.appendChild(icon);
btn.appendChild(document.createTextNode(' ' + BUTTON_LABEL));
}
function setButtonLoading(btn) {
btn.textContent = '';
const spinner = document.createElement('span');
spinner.className = 'spinner-border spinner-border-sm me-2';
btn.appendChild(spinner);
btn.appendChild(document.createTextNode(' Verifying...'));
}
document.addEventListener('DOMContentLoaded', () => {
const verifyBtn = document.getElementById('verifyPasskeyBtn');
const errorEl = document.getElementById('challengeError');
if (!verifyBtn) return;
if (!isWebAuthnSupported()) {
verifyBtn.disabled = true;
showMessage(errorEl,
'Your browser does not support passkeys. Please use a different browser or contact support.',
'alert-danger');
return;
}
verifyBtn.addEventListener('click', async () => {
verifyBtn.disabled = true;
setButtonLoading(verifyBtn);
errorEl.classList.add('d-none');
try {
const redirectUrl = await authenticateWithPasskey();
window.location.href = redirectUrl;
} catch (error) {
console.error('MFA WebAuthn challenge failed:', error);
showMessage(errorEl,
'Verification failed. Please try again or cancel and sign out.',
'alert-danger');
verifyBtn.disabled = false;
setButtonReady(verifyBtn);
}
});
});