From e15271874b14e34933859b991fe36a5e7f898884 Mon Sep 17 00:00:00 2001 From: Deon George Date: Mon, 22 Jul 2024 22:33:31 +1000 Subject: [PATCH] Moved create passkey javascript into js/passkey.js --- README.md | 34 +++------------------------------- src/public/js/passkey.js | 39 ++++++++++++++++++++++++++++++++++++--- 2 files changed, 39 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index 5efcd9e..8567731 100644 --- a/README.md +++ b/README.md @@ -9,35 +9,7 @@ @@ -50,6 +22,6 @@ -``` \ No newline at end of file +``` diff --git a/src/public/js/passkey.js b/src/public/js/passkey.js index 8cc55c2..7c2b611 100644 --- a/src/public/js/passkey.js +++ b/src/public/js/passkey.js @@ -82,7 +82,7 @@ function passkey_check_browser() /** * Register/Create a passkey for a user */ -async function passkey_register(csrf_token,icon_dom,icon,icon_shell_success,icon_shell_fail) +async function passkey_register(csrf_token,icon_dom,icon,icon_shell_current,icon_shell_success,icon_shell_fail) { try { if (! passkey_check_browser()) @@ -135,7 +135,7 @@ async function passkey_register(csrf_token,icon_dom,icon,icon_shell_success,icon console.log('Passkey: Registration Success'); icon_dom.find('i').addClass(icon).removeClass('spinner-grow spinner-grow-sm'); - icon_dom.removeClass('btn-outline-primary').addClass('btn-primary'); + icon_dom.removeClass(icon_shell_current).addClass(icon_shell_success); }, error: function(e,status,error) { throw new Error(status || 'Unknown error occurred'); @@ -147,7 +147,7 @@ async function passkey_register(csrf_token,icon_dom,icon,icon_shell_success,icon console.log(status || 'Passkey: User Aborted Register'); // Restore the icon - icon_dom.find('i').addClass(icon).removeClass('spinner-grow spinner-grow-sm'); + icon_dom.removeClass(icon_shell_current).addClass(icon_shell_fail).find('i').addClass(icon).removeClass('spinner-grow spinner-grow-sm'); return; } @@ -222,4 +222,37 @@ async function passkey_check(csrf_token,redirect) } catch (err) { window.alert(err || 'An UNKNOWN error occurred?'); } +} + +function passkey_create(object,csrf,icon,icon_class_current,icon_class_success,icon_class_nop) +{ + if (passkey_debug) + console.log('Passkey: Create Click'); + + // Availability of `window.PublicKeyCredential` means WebAuthn is usable. + // `isUserVerifyingPlatformAuthenticatorAvailable` means the feature detection is usable. + // `sConditionalMediationAvailable` means the feature detection is usable. + if (window.PublicKeyCredential && + PublicKeyCredential.isUserVerifyingPlatformAuthenticatorAvailable && + PublicKeyCredential.isConditionalMediationAvailable) { + // Check if user verifying platform authenticator is available. + Promise.all([ + PublicKeyCredential.isUserVerifyingPlatformAuthenticatorAvailable(), + PublicKeyCredential.isConditionalMediationAvailable(), + ]).then(results => { + if (passkey_debug) + console.log('Passkey: Browser Supported'); + + if (results.every(r => r === true)) { + passkey_register(csrf,object,icon,icon_class_current,icon_class_success,icon_class_nop); + } else { + alert('It seems that passkey is NOT supported by your browse (B)'); + } + }); + + } else { + alert('It seems that passkey is NOT supported by your browser (A)'); + } + + return false; } \ No newline at end of file