Skip to content

Commit a2fdf6f

Browse files
committed
Use async-await in authenticate
1 parent 2f806bb commit a2fdf6f

File tree

1 file changed

+16
-13
lines changed

1 file changed

+16
-13
lines changed

webauthn-server-demo/src/main/webapp/index.html

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -446,22 +446,25 @@
446446
function authenticateWithUsername(event) {
447447
return authenticate(event, document.getElementById('username').value);
448448
}
449-
function authenticate(event, username) {
450-
return performCeremony({
451-
getIndexActions,
452-
getRequest: urls => getAuthenticateRequest(urls, username),
453-
statusStrings: {
454-
init: 'Initiating authentication ceremony with server...',
455-
authenticatorRequest: 'Asking authenticators to perform assertion...',
456-
success: 'Authentication successful!',
457-
},
458-
executeRequest: executeAuthenticateRequest,
459-
}).then(data => {
449+
async function authenticate(event, username) {
450+
try {
451+
const data = await performCeremony({
452+
getIndexActions,
453+
getRequest: urls => getAuthenticateRequest(urls, username),
454+
statusStrings: {
455+
init: 'Initiating authentication ceremony with server...',
456+
authenticatorRequest: 'Asking authenticators to perform assertion...',
457+
success: 'Authentication successful!',
458+
},
459+
executeRequest: executeAuthenticateRequest,
460+
});
461+
460462
if (data.registrations) {
461463
addMessage(`Authenticated as: ${data.registrations[0].username}`);
462464
}
463465
return data;
464-
}).catch((err) => {
466+
467+
} catch (err) {
465468
setStatus('Authentication failed.');
466469
if (err.name === 'InvalidStateError') {
467470
addMessage(`This authenticator is not registered for the account "${username}". Please try again with a registered authenticator.`)
@@ -472,7 +475,7 @@
472475
}
473476
console.error('Authentication failed', err);
474477
return rejected(err);
475-
});
478+
}
476479
}
477480

478481
function deregister() {

0 commit comments

Comments
 (0)