Moved create passkey javascript into js/passkey.js

This commit is contained in:
Deon George 2024-07-22 22:33:31 +10:00
parent 4872d55ed6
commit e15271874b
2 changed files with 39 additions and 34 deletions

View File

@ -9,35 +9,7 @@
<script type="text/javascript">
$(document).ready(function() {
$('#passkey').on('click',function(item) {
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_token() }}',$(this),'bi-key','btn-primary','{{ $o->passkey ? 'btn-primary' : 'btn-outline-primary' }}');
} 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;
return passkey_create($(this),'{{ csrf_token() }}','fa-key','{{ ($o??$user)->passkey ? 'btn-success' : 'btn-outline-secondary' }}','btn-success','{{ ($o??$user)->passkey ? 'btn-secondary' : 'btn-outline-secondary' }}');
});
});
</script>
@ -50,6 +22,6 @@
<script type='text/javascript' src='{{ asset('/passkey/passkey.js') }}'></script>
<script type="text/javascript">
passkey_check('{{ csrf_token() }}','{{ back()->getTargetUrl() }}');
passkey_check('{{ csrf_token() }}','/passkey/loggedin');
</script>
```

View File

@ -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;
}
@ -223,3 +223,36 @@ async function passkey_check(csrf_token,redirect)
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;
}