Same pattern as GameStream (commit 6a46007), adapted for CollabTable being genuinely multi-user (not single-admin): admin user management (list/create/update/delete, role reassignment, superadmin protection) is fully proxied to AuthGuard, which already enforces the same rules CollabTable checked locally. Cookie forwarding, no local session token, AUTHGUARD_URL optional (unset = auth fully disabled), transport failures always surface as errAuthGuardUnavailable rather than ever letting a request through - proven with live tests against a closed socket. CollabTable's device-sync API token feature (separate from browser sessions) is deliberately left local-only regardless of AUTHGUARD_URL, keyed by a new currentUserID helper (AuthGuard's id when enabled, the shared placeholder id 0 when disabled) - a real, functional token either way, not faked data. Real bug found and fixed: api_tokens.user_id had a hard FK into the local auth_users table, which would reject new tokens once accounts stop being written locally. Dropped the now-invalid FK. Known, accepted gap (same as GameStream): self-service profile editing for non-admin accounts is lost, since AuthGuard only exposes an admin endpoint, no self-edit. TOTP QR issuer branding now comes from AuthGuard rather than CollabTable. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
185 lines
6.1 KiB
JavaScript
185 lines
6.1 KiB
JavaScript
// Shared confirm dialog utility — matches the dark design language of the monorepo.
|
|
// Usage: const ok = await NCConfirm.show({ title, message, confirmLabel, cancelLabel, danger });
|
|
// Returns a Promise<boolean>: true = confirmed, false = cancelled.
|
|
|
|
(function (global) {
|
|
if (global.NCConfirm) return;
|
|
|
|
const DIALOG_ID = 'nc-confirm-dialog';
|
|
|
|
function ensureDialog() {
|
|
let dialog = document.getElementById(DIALOG_ID);
|
|
if (dialog) return dialog;
|
|
|
|
dialog = document.createElement('div');
|
|
dialog.id = DIALOG_ID;
|
|
dialog.setAttribute('role', 'dialog');
|
|
dialog.setAttribute('aria-modal', 'true');
|
|
dialog.setAttribute('hidden', '');
|
|
dialog.style.display = 'none';
|
|
// Backdrop + centering
|
|
dialog.className = 'fixed inset-0 z-[10100] flex items-center justify-center p-4';
|
|
dialog.style.background = 'rgba(0,0,0,0.65)';
|
|
dialog.style.backdropFilter = 'blur(2px)';
|
|
|
|
dialog.innerHTML = `
|
|
<div
|
|
id="nc-confirm-panel"
|
|
class="w-full max-w-sm rounded-lg border border-slate-700 bg-slate-900 p-4 shadow-2xl"
|
|
role="document"
|
|
>
|
|
<h2
|
|
id="nc-confirm-title"
|
|
class="mb-2 text-base font-semibold text-white"
|
|
></h2>
|
|
<p
|
|
id="nc-confirm-message"
|
|
class="mb-4 text-sm leading-relaxed text-slate-300"
|
|
></p>
|
|
<div class="flex justify-end gap-3">
|
|
<button
|
|
id="nc-confirm-cancel"
|
|
type="button"
|
|
class="inline-flex items-center rounded-lg border border-slate-600 px-4 py-2 text-sm font-medium text-slate-300 transition hover:bg-slate-800 focus:outline-none focus:ring-2 focus:ring-blue-300/20"
|
|
></button>
|
|
<button
|
|
id="nc-confirm-ok"
|
|
type="button"
|
|
class="inline-flex items-center rounded-lg px-4 py-2 text-sm font-medium text-white transition focus:outline-none focus:ring-2 focus:ring-offset-1 focus:ring-offset-slate-900"
|
|
></button>
|
|
</div>
|
|
</div>
|
|
`;
|
|
|
|
document.body.appendChild(dialog);
|
|
return dialog;
|
|
}
|
|
|
|
let _resolve = null;
|
|
let _lastActive = null;
|
|
|
|
function focusables(dialog) {
|
|
return [
|
|
...dialog.querySelectorAll(
|
|
'button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])'
|
|
),
|
|
].filter(el => !el.disabled && el.offsetParent !== null);
|
|
}
|
|
|
|
function onKeyDown(e) {
|
|
const dialog = document.getElementById(DIALOG_ID);
|
|
if (!dialog || dialog.style.display === 'none') return;
|
|
|
|
if (e.key === 'Escape') {
|
|
e.preventDefault();
|
|
close(false);
|
|
return;
|
|
}
|
|
|
|
if (e.key === 'Tab') {
|
|
const els = focusables(dialog);
|
|
if (!els.length) return;
|
|
const first = els[0];
|
|
const last = els[els.length - 1];
|
|
if (e.shiftKey && document.activeElement === first) {
|
|
e.preventDefault();
|
|
last.focus();
|
|
} else if (!e.shiftKey && document.activeElement === last) {
|
|
e.preventDefault();
|
|
first.focus();
|
|
}
|
|
}
|
|
}
|
|
|
|
function close(result) {
|
|
const dialog = document.getElementById(DIALOG_ID);
|
|
if (dialog) {
|
|
dialog.style.display = 'none';
|
|
}
|
|
document.removeEventListener('keydown', onKeyDown);
|
|
if (_lastActive && typeof _lastActive.focus === 'function') {
|
|
try {
|
|
_lastActive.focus();
|
|
} catch {
|
|
/* ignore */
|
|
}
|
|
}
|
|
_lastActive = null;
|
|
if (_resolve) {
|
|
const r = _resolve;
|
|
_resolve = null;
|
|
r(result);
|
|
}
|
|
}
|
|
|
|
function show({
|
|
title = 'Confirm',
|
|
message = '',
|
|
confirmLabel = 'Confirm',
|
|
cancelLabel = 'Cancel',
|
|
danger = false,
|
|
} = {}) {
|
|
return new Promise(resolve => {
|
|
const dialog = ensureDialog();
|
|
|
|
document.getElementById('nc-confirm-title').textContent = title;
|
|
document.getElementById('nc-confirm-message').textContent = message;
|
|
document.getElementById('nc-confirm-cancel').textContent = cancelLabel;
|
|
|
|
const okBtn = document.getElementById('nc-confirm-ok');
|
|
okBtn.textContent = confirmLabel;
|
|
|
|
// Danger vs primary style for confirm button
|
|
if (danger) {
|
|
okBtn.className = okBtn.className.replace(/focus:ring-\S+/g, 'focus:ring-red-500/50');
|
|
okBtn.style.background = 'var(--color-danger-strong, #ef4444)';
|
|
okBtn.onmouseover = () => {
|
|
okBtn.style.background = 'var(--color-danger-hover, #dc2626)';
|
|
};
|
|
okBtn.onmouseout = () => {
|
|
okBtn.style.background = 'var(--color-danger-strong, #ef4444)';
|
|
};
|
|
} else {
|
|
okBtn.style.background = 'var(--color-action, #2563eb)';
|
|
okBtn.onmouseover = () => {
|
|
okBtn.style.background = 'var(--color-action-hover, #1d4ed8)';
|
|
};
|
|
okBtn.onmouseout = () => {
|
|
okBtn.style.background = 'var(--color-action, #2563eb)';
|
|
};
|
|
// Reset ring class in case it was set to danger previously
|
|
const cls = okBtn.className;
|
|
if (!cls.includes('focus:ring-blue')) {
|
|
okBtn.className = cls.replace(/focus:ring-\S+/g, 'focus:ring-blue-300/50');
|
|
}
|
|
}
|
|
|
|
// Wire up resolve
|
|
_resolve = resolve;
|
|
_lastActive = document.activeElement;
|
|
|
|
// Re-attach listeners (removes stale ones first)
|
|
const cancelBtn = document.getElementById('nc-confirm-cancel');
|
|
const freshCancel = cancelBtn.cloneNode(true);
|
|
cancelBtn.parentNode.replaceChild(freshCancel, cancelBtn);
|
|
const freshOk = okBtn.cloneNode(true);
|
|
okBtn.parentNode.replaceChild(freshOk, okBtn);
|
|
|
|
// Re-query after replacement
|
|
document.getElementById('nc-confirm-cancel').addEventListener('click', () => close(false));
|
|
document.getElementById('nc-confirm-ok').addEventListener('click', () => close(true));
|
|
|
|
document.addEventListener('keydown', onKeyDown);
|
|
|
|
dialog.style.display = '';
|
|
// Move focus to confirm button after paint
|
|
setTimeout(() => {
|
|
const btn = document.getElementById('nc-confirm-ok');
|
|
if (btn) btn.focus();
|
|
}, 10);
|
|
});
|
|
}
|
|
|
|
global.NCConfirm = { show };
|
|
})(window);
|