Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions web_src/js/features/user-auth-webauthn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,19 @@ import {GET, POST} from '../modules/fetch.ts';

const {appSubUrl} = window.config;

/** One of the possible values for the `data-webauthn-error-msg` attribute on the webauthn error message element */
type ErrorType = 'general' | 'insecure' | 'browser' | 'unable-to-process' | 'duplicated' | 'unknown';

export async function initUserAuthWebAuthn() {
const elPrompt = document.querySelector('.user.signin.webauthn-prompt');
const elSignInPasskeyBtn = document.querySelector('.signin-passkey');
if (!elPrompt && !elSignInPasskeyBtn) {
return;
}

// webauthn is only supported on secure contexts
if (!window.isSecureContext) {
hideElem(elSignInPasskeyBtn);
return;
}

if (!detectWebAuthnSupport()) {
const errorType = detectWebAuthnSupport();
if (errorType) {
if (elSignInPasskeyBtn) hideElem(elSignInPasskeyBtn);
return;
}

Expand Down Expand Up @@ -182,8 +181,8 @@ async function webauthnRegistered(newCredential: any) { // TODO: Credential type
window.location.reload();
}

function webAuthnError(errorType: string, message:string = '') {
const elErrorMsg = document.querySelector(`#webauthn-error-msg`);
function webAuthnError(errorType: ErrorType, message:string = '') {
const elErrorMsg = document.querySelector(`#webauthn-error-msg`)!;

if (errorType === 'general') {
elErrorMsg.textContent = message || 'unknown error';
Expand All @@ -199,25 +198,26 @@ function webAuthnError(errorType: string, message:string = '') {
showElem('#webauthn-error');
}

function detectWebAuthnSupport() {
/** Returns the error type or `null` when there was no error. */
function detectWebAuthnSupport(): ErrorType | null {
if (!window.isSecureContext) {
webAuthnError('insecure');
return false;
return 'insecure';
}

if (typeof window.PublicKeyCredential !== 'function') {
webAuthnError('browser');
return false;
return 'browser';
}

return true;
return null;
}

export function initUserAuthWebAuthnRegister() {
const elRegister = document.querySelector<HTMLInputElement>('#register-webauthn');
if (!elRegister) return;

if (!detectWebAuthnSupport()) {
const errorType = detectWebAuthnSupport();
if (errorType) {
webAuthnError(errorType);
elRegister.disabled = true;
return;
}
Expand Down