import { startAuthentication } from '@simplewebauthn/browser';
import $ from 'jquery';
import { ActionDialog } from 'vj/components/dialog';
import Notification from 'vj/components/notification';
import { AutoloadPage } from 'vj/misc/Page';
import { i18n, request, tpl } from 'vj/utils';
async function verifywebauthn($form) {
if (!window.isSecureContext || !('credentials' in navigator)) {
Notification.error(i18n('Your browser does not support WebAuthn or you are not in secure context.'));
return null;
}
let uname = '';
if ($form['uname']) uname = $form['uname'].value;
const authnInfo = await request.get('/user/webauthn', uname ? { uname } : undefined);
if (!authnInfo.authOptions) {
Notification.error(i18n('Failed to fetch registration data.'));
return null;
}
Notification.info(i18n('Please follow the instructions on your device to complete the verification.'));
const result = await startAuthentication({ optionsJSON: authnInfo.authOptions })
.catch((e) => {
Notification.error(i18n('Failed to get credential: {0}', e));
return null;
});
if (!result) return null;
try {
const authn = await request.post('/user/webauthn', {
result,
});
if (!authn.error) return authnInfo.authOptions.challenge;
} catch (err) {
Notification.error(err.message);
console.error(err);
}
return null;
}
async function chooseAction(authn?: boolean) {
return await new ActionDialog({
$body: tpl`
${i18n('Two Factor Authentication')}
${i18n('Your account has two factor authentication enabled. Please choose an authenticator to verify.')}