mirror of
https://github.com/whoswhip/Sharpbin.git
synced 2026-12-06 02:48:43 +00:00
feat: totp 2fa; httpcontext extension for jwtuser; asp.net data protection; prevent banned users from certain actions; account deletion requires 2fa if enabled; making users admins requires 2fa if enabled; httpcontext extension to get ip; enhance user store
refactor: organize dtos by domain; verification providers use verification context; move ip parsing into http context extensions chore: clean code
This commit is contained in:
@@ -1,6 +1,17 @@
|
||||
<script lang="ts">
|
||||
import QRCode from 'qrcode';
|
||||
import { getRefreshToken, getToken, setTokens } from '$lib/utils/auth';
|
||||
import { parseTotpEnabled } from '$lib/utils/totp';
|
||||
|
||||
export let show = false;
|
||||
export let mode: 'decrypt' | 'encrypt' | 'confirm' | 'prompt' | 'multiselect' = 'decrypt';
|
||||
export let mode:
|
||||
| 'decrypt'
|
||||
| 'encrypt'
|
||||
| 'confirm'
|
||||
| 'prompt'
|
||||
| 'multiselect'
|
||||
| 'totp'
|
||||
| 'totpSetup' = 'decrypt';
|
||||
export let title = '';
|
||||
export let message = '';
|
||||
export let error = '';
|
||||
@@ -8,26 +19,189 @@
|
||||
export let inputType = 'text';
|
||||
export let items: { label: string; value: unknown }[] = [];
|
||||
export let initialValue: unknown = null;
|
||||
export let totpActive: boolean | null = null;
|
||||
export let onConfirm: (value: unknown) => void;
|
||||
export let onCancel: () => void;
|
||||
export let confirmButtonText = '';
|
||||
|
||||
let inputValue: string | unknown[] | boolean = '';
|
||||
let totpSecret = '';
|
||||
let totpQr = '';
|
||||
let totpEnabled = false;
|
||||
let totpInitialized = false;
|
||||
let totpLoading = false;
|
||||
let totpError = '';
|
||||
let displayError = '';
|
||||
|
||||
$: if (!show) {
|
||||
$: displayError = error || totpError;
|
||||
|
||||
$: if (!show) resetModal();
|
||||
|
||||
$: if (show && mode === 'totpSetup' && !totpInitialized) {
|
||||
totpInitialized = true;
|
||||
if (totpActive !== null) {
|
||||
totpEnabled = totpActive;
|
||||
}
|
||||
initializeTotp();
|
||||
}
|
||||
|
||||
function resetModal() {
|
||||
inputValue =
|
||||
initialValue !== null
|
||||
? JSON.parse(JSON.stringify(initialValue))
|
||||
: mode === 'multiselect'
|
||||
? []
|
||||
: '';
|
||||
totpSecret = '';
|
||||
totpQr = '';
|
||||
totpEnabled = false;
|
||||
totpInitialized = false;
|
||||
totpLoading = false;
|
||||
totpError = '';
|
||||
}
|
||||
|
||||
function handleSubmit() {
|
||||
if (mode === 'confirm') {
|
||||
onConfirm(true);
|
||||
} else {
|
||||
onConfirm(inputValue);
|
||||
function getCodeOrError(message: string) {
|
||||
if (typeof inputValue !== 'string') {
|
||||
totpError = message;
|
||||
return null;
|
||||
}
|
||||
const code = inputValue.trim();
|
||||
if (!code) {
|
||||
totpError = message;
|
||||
return null;
|
||||
}
|
||||
return code;
|
||||
}
|
||||
|
||||
function onCodeInput(e: Event) {
|
||||
if (!(e.target instanceof HTMLInputElement)) return;
|
||||
const sanitized = e.target.value.replace(/\D/g, '').slice(0, 6);
|
||||
e.target.value = sanitized;
|
||||
inputValue = sanitized;
|
||||
}
|
||||
|
||||
async function initializeTotp() {
|
||||
totpError = '';
|
||||
totpLoading = true;
|
||||
const token = getToken();
|
||||
if (!token) {
|
||||
totpError = 'You need to be logged in to manage TOTP.';
|
||||
totpLoading = false;
|
||||
return;
|
||||
}
|
||||
if (totpEnabled || parseTotpEnabled(token)) {
|
||||
totpEnabled = true;
|
||||
totpLoading = false;
|
||||
return;
|
||||
}
|
||||
const res = await fetch('/api/auth/totp/enroll', {
|
||||
headers: { Authorization: `Bearer ${token}` }
|
||||
});
|
||||
if (!res.ok) {
|
||||
const data = await res.json().catch(() => null);
|
||||
totpError = data?.message || 'Failed to start TOTP enrollment.';
|
||||
totpLoading = false;
|
||||
return;
|
||||
}
|
||||
const data = await res.json();
|
||||
totpSecret = data.secret || '';
|
||||
if (data.otpauth) {
|
||||
totpQr = await QRCode.toDataURL(data.otpauth, { margin: 1, width: 300 });
|
||||
}
|
||||
totpLoading = false;
|
||||
}
|
||||
|
||||
async function refreshTokens() {
|
||||
const token = getToken();
|
||||
const refreshToken = getRefreshToken();
|
||||
if (!token || !refreshToken) return null;
|
||||
const res = await fetch('/api/auth/refresh', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ token, refreshToken })
|
||||
});
|
||||
if (!res.ok) return null;
|
||||
const data = await res.json();
|
||||
if (data.token?.token && data.token?.refreshToken && data.token?.success !== false) {
|
||||
setTokens(data.token.token, data.token.refreshToken);
|
||||
return data.token.token as string;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
async function handleSubmit() {
|
||||
if (mode === 'totp') {
|
||||
if (!inputValue || typeof inputValue !== 'string') return;
|
||||
onConfirm(inputValue.trim());
|
||||
return;
|
||||
}
|
||||
if (mode === 'totpSetup') {
|
||||
await submitTotp();
|
||||
return;
|
||||
}
|
||||
onConfirm(mode === 'confirm' ? true : inputValue);
|
||||
}
|
||||
|
||||
async function submitTotp() {
|
||||
if (totpEnabled) {
|
||||
await disableTotp();
|
||||
return;
|
||||
}
|
||||
if (!totpSecret) {
|
||||
totpError = 'Missing TOTP secret. Please reopen the modal.';
|
||||
return;
|
||||
}
|
||||
const code = getCodeOrError('Enter the TOTP code from your authenticator app.');
|
||||
if (!code) return;
|
||||
const token = getToken();
|
||||
if (!token) {
|
||||
totpError = 'You need to be logged in to enable TOTP.';
|
||||
return;
|
||||
}
|
||||
const res = await fetch('/api/auth/totp/enable', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${token}` },
|
||||
body: JSON.stringify({ secret: totpSecret, totpcode: code })
|
||||
});
|
||||
if (!res.ok) {
|
||||
const data = await res.json().catch(() => null);
|
||||
totpError = data?.message || 'Failed to enable TOTP.';
|
||||
return;
|
||||
}
|
||||
const newToken = await refreshTokens();
|
||||
totpEnabled = parseTotpEnabled(newToken || getToken());
|
||||
onConfirm({ enabled: totpEnabled, secret: totpSecret });
|
||||
}
|
||||
|
||||
async function disableTotp() {
|
||||
const code = getCodeOrError('Enter the TOTP code to disable two-factor authentication.');
|
||||
if (!code) return;
|
||||
const token = getToken();
|
||||
if (!token) {
|
||||
totpError = 'You need to be logged in to disable TOTP.';
|
||||
return;
|
||||
}
|
||||
const res = await fetch('/api/auth/totp/disable', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${token}` },
|
||||
body: JSON.stringify({ totpcode: code })
|
||||
});
|
||||
if (!res.ok) {
|
||||
const data = await res.json().catch(() => null);
|
||||
totpError = data?.message || 'Failed to disable TOTP.';
|
||||
return;
|
||||
}
|
||||
const newToken = await refreshTokens();
|
||||
totpEnabled = parseTotpEnabled(newToken || getToken());
|
||||
onConfirm({ enabled: totpEnabled });
|
||||
}
|
||||
|
||||
async function copySecret() {
|
||||
if (!totpSecret) return;
|
||||
try {
|
||||
await navigator.clipboard.writeText(totpSecret);
|
||||
} catch {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,7 +215,11 @@
|
||||
? 'Confirm action'
|
||||
: mode === 'multiselect'
|
||||
? 'Select items'
|
||||
: 'Enter value');
|
||||
: mode === 'totpSetup'
|
||||
? 'Set up two-factor authentication'
|
||||
: mode === 'totp'
|
||||
? 'Enter TOTP code'
|
||||
: 'Enter value');
|
||||
|
||||
$: confirmLabel =
|
||||
confirmButtonText ||
|
||||
@@ -51,8 +229,12 @@
|
||||
? 'Encrypt'
|
||||
: mode === 'confirm'
|
||||
? 'Delete'
|
||||
: 'Confirm');
|
||||
$: shouldShowInput = mode !== 'confirm' && mode !== 'multiselect';
|
||||
: mode === 'totpSetup'
|
||||
? totpEnabled
|
||||
? 'Disable'
|
||||
: 'Enable'
|
||||
: 'Confirm');
|
||||
$: shouldShowInput = mode !== 'confirm' && mode !== 'multiselect' && mode !== 'totpSetup';
|
||||
</script>
|
||||
|
||||
{#if show}
|
||||
@@ -71,7 +253,58 @@
|
||||
</p>
|
||||
{/if}
|
||||
|
||||
{#if shouldShowInput}
|
||||
{#if mode === 'totpSetup'}
|
||||
<div class="mb-4 space-y-3">
|
||||
<div
|
||||
class="rounded border border-neutral-700 bg-neutral-800 p-3 text-sm text-neutral-200"
|
||||
>
|
||||
{totpEnabled
|
||||
? 'Two-factor authentication is enabled. Enter a current 6-digit code to disable it.'
|
||||
: 'Scan the QR code with your authenticator app or copy the secret to add it manually.'}
|
||||
</div>
|
||||
{#if totpLoading}
|
||||
<p class="text-neutral-300">Preparing TOTP enrollment...</p>
|
||||
{:else}
|
||||
{#if !totpEnabled}
|
||||
{#if totpQr}
|
||||
<div class="flex justify-center">
|
||||
<img
|
||||
src={totpQr}
|
||||
alt="TOTP QR code"
|
||||
class="aspect-square w-full rounded bg-white object-contain p-2"
|
||||
/>
|
||||
</div>
|
||||
{/if}
|
||||
{#if totpSecret}
|
||||
<div
|
||||
class="flex items-center gap-2 rounded border border-neutral-700 bg-neutral-800 p-2"
|
||||
>
|
||||
<span class="flex-1 truncate font-mono text-sm text-neutral-100"
|
||||
>{totpSecret}</span
|
||||
>
|
||||
<button
|
||||
type="button"
|
||||
on:click={copySecret}
|
||||
class="rounded bg-neutral-700 px-3 py-1 text-sm font-medium text-white transition-colors hover:bg-neutral-800"
|
||||
>
|
||||
Copy
|
||||
</button>
|
||||
</div>
|
||||
{/if}
|
||||
{/if}
|
||||
<input
|
||||
type="text"
|
||||
bind:value={inputValue}
|
||||
placeholder="Enter the 6-digit code"
|
||||
maxlength="6"
|
||||
inputmode="numeric"
|
||||
pattern="[0-9]*"
|
||||
on:input={onCodeInput}
|
||||
class="w-full rounded border border-neutral-700 bg-neutral-800 p-2 text-white outline-none"
|
||||
/>
|
||||
{/if}
|
||||
</div>
|
||||
{:else if shouldShowInput}
|
||||
<input
|
||||
type={inputType}
|
||||
bind:value={inputValue}
|
||||
@@ -100,9 +333,9 @@
|
||||
</div>
|
||||
{/if}
|
||||
|
||||
{#if error}
|
||||
{#if displayError}
|
||||
<div class="mb-3 rounded border border-red-900 bg-red-950 p-2 text-sm text-red-200">
|
||||
{error}
|
||||
{displayError}
|
||||
</div>
|
||||
{/if}
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { writable } from 'svelte/store';
|
||||
import type { User } from '$lib/types/user';
|
||||
import type { AuthUser } from '$lib/types/user';
|
||||
|
||||
export const user = writable<User | null>(null);
|
||||
export const user = writable<AuthUser | null>(null);
|
||||
|
||||
@@ -13,3 +13,7 @@ export interface User {
|
||||
pastes: Paste[] | null;
|
||||
pagination: Pagination | null;
|
||||
}
|
||||
|
||||
export interface AuthUser extends User {
|
||||
totpEnabled: boolean;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
export function parseTotpEnabled(tokenValue: string | null) {
|
||||
if (!tokenValue) return false;
|
||||
try {
|
||||
const payload = JSON.parse(atob(tokenValue.split('.')[1]));
|
||||
const flag = payload?.TOTP_Enabled ?? payload?.totp_enabled ?? payload?.totpEnabled;
|
||||
if (typeof flag === 'string') return flag.toLowerCase() === 'true';
|
||||
return Boolean(flag);
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
export function needsAdminTotp(totpEnabled: boolean, currentRoles: number[], nextRoles: number[]) {
|
||||
if (!totpEnabled) return false;
|
||||
const nextHasAdmin = nextRoles.includes(255);
|
||||
const alreadyAdmin = currentRoles.includes(255);
|
||||
return nextHasAdmin && !alreadyAdmin;
|
||||
}
|
||||
@@ -5,6 +5,7 @@
|
||||
import Navbar from '$lib/components/Navbar.svelte';
|
||||
import { user } from '$lib/stores/user';
|
||||
import { startTokenRefreshInterval, getToken, refreshTokenIfNeeded } from '$lib/utils/auth';
|
||||
import { parseTotpEnabled } from '$lib/utils/totp';
|
||||
import { page } from '$app/state';
|
||||
import type { LayoutData } from './$types';
|
||||
|
||||
@@ -27,7 +28,8 @@
|
||||
});
|
||||
|
||||
if (res.ok) {
|
||||
user.set(await res.json());
|
||||
const me = await res.json();
|
||||
user.set({ ...me, totpEnabled: parseTotpEnabled(getToken()) });
|
||||
} else {
|
||||
user.set(null);
|
||||
}
|
||||
|
||||
@@ -82,7 +82,11 @@
|
||||
}
|
||||
window.location.href = `/${result.id}`;
|
||||
} else {
|
||||
error = 'Failed to create paste. Please try again.';
|
||||
if (response.status === 403) {
|
||||
error = 'You are banned from creating new pastes.';
|
||||
} else {
|
||||
error = 'Failed to create paste. Please try again.';
|
||||
}
|
||||
setInterval(() => {
|
||||
error = '';
|
||||
}, 5000);
|
||||
@@ -152,7 +156,8 @@
|
||||
{/if}
|
||||
<button
|
||||
type="submit"
|
||||
class="w-full cursor-pointer rounded bg-neutral-700 px-4 py-2 font-semibold text-white transition-colors duration-200 hover:bg-neutral-800 active:bg-neutral-900"
|
||||
disabled={$user?.roles && $user.roles.includes(403)}
|
||||
class="w-full cursor-pointer rounded bg-neutral-700 px-4 py-2 font-semibold text-white transition-colors duration-200 hover:bg-neutral-800 active:bg-neutral-900 disabled:cursor-not-allowed disabled:bg-neutral-950/50 disabled:text-neutral-400"
|
||||
>Create Paste</button
|
||||
>
|
||||
{#if error}
|
||||
@@ -163,6 +168,14 @@
|
||||
{error}
|
||||
</div>
|
||||
{/if}
|
||||
{#if $user?.roles && $user.roles.includes(403)}
|
||||
<div
|
||||
transition:fly={{ y: 40, duration: 300 }}
|
||||
class="mt-2 rounded border border-red-900 bg-red-950 p-2 text-sm text-red-200"
|
||||
>
|
||||
You are banned from creating new pastes.
|
||||
</div>
|
||||
{/if}
|
||||
</form>
|
||||
</div>
|
||||
</main>
|
||||
|
||||
@@ -7,6 +7,8 @@
|
||||
let username = '';
|
||||
let password = '';
|
||||
let error = '';
|
||||
let totpEnabled = false;
|
||||
let totpCode = '';
|
||||
export let data: PageData;
|
||||
|
||||
onMount(() => {
|
||||
@@ -31,7 +33,7 @@
|
||||
});
|
||||
|
||||
async function login() {
|
||||
const body: { username: string; password: string; token?: string } = {
|
||||
const body: { username: string; password: string; token?: string; totpcode?: string } = {
|
||||
username,
|
||||
password
|
||||
};
|
||||
@@ -43,6 +45,10 @@
|
||||
}
|
||||
}
|
||||
|
||||
if (totpEnabled && totpCode) {
|
||||
body.totpcode = totpCode;
|
||||
}
|
||||
|
||||
const res = await fetch('/api/auth/login', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
@@ -67,6 +73,14 @@
|
||||
const messages = Object.values(resData.errors).flat();
|
||||
error = messages.join('\n');
|
||||
} else {
|
||||
if (resData.message === 'TOTP code is required.') {
|
||||
totpEnabled = true;
|
||||
}
|
||||
if (resData.message === 'Verification failed.') {
|
||||
if (window.turnstile) {
|
||||
window.turnstile.reset();
|
||||
}
|
||||
}
|
||||
error = resData.message || 'Login failed. Please try again.';
|
||||
}
|
||||
}
|
||||
@@ -95,6 +109,24 @@
|
||||
autocomplete="current-password"
|
||||
class="focus:bg-neutral-750 w-full rounded border border-neutral-700 bg-neutral-800 p-2 placeholder-neutral-500 transition-colors duration-200 focus:border-neutral-600"
|
||||
/>
|
||||
{#if totpEnabled}
|
||||
<input
|
||||
type="text"
|
||||
placeholder="TOTP Code"
|
||||
bind:value={totpCode}
|
||||
maxlength="6"
|
||||
inputmode="numeric"
|
||||
pattern="[0-9]*"
|
||||
required
|
||||
on:input={(e) => {
|
||||
if (e.target instanceof HTMLInputElement) {
|
||||
e.target.value = e.target.value.replace(/\D/g, '').slice(0, 6);
|
||||
totpCode = e.target.value;
|
||||
}
|
||||
}}
|
||||
class="focus:bg-neutral-750 w-full rounded border border-neutral-700 bg-neutral-800 p-2 placeholder-neutral-500 transition-colors duration-200 focus:border-neutral-600"
|
||||
/>
|
||||
{/if}
|
||||
{#if data.options?.cf_turnstile_site_key}
|
||||
<div class="flex w-full justify-center">
|
||||
<div class="cf-turnstile"></div>
|
||||
|
||||
@@ -100,6 +100,11 @@
|
||||
const messages = Object.values(resData.errors).flat();
|
||||
error = messages.join('\n');
|
||||
} else {
|
||||
if (resData.message === 'Verification failed.') {
|
||||
if (window.turnstile) {
|
||||
window.turnstile.reset();
|
||||
}
|
||||
}
|
||||
error = resData.message || 'Registration failed. Please try again.';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
import { extractDateFromUUIDv7, tooltip, dateToRelativeString } from '$lib/utils/misc';
|
||||
import { roles } from '$lib/consts';
|
||||
import { getToken } from '$lib/utils/auth';
|
||||
import { needsAdminTotp } from '$lib/utils/totp';
|
||||
import Modal from '$lib/components/Modal.svelte';
|
||||
import { user } from '$lib/stores/user';
|
||||
import { onMount } from 'svelte';
|
||||
@@ -36,11 +37,19 @@
|
||||
let showEditModal = false;
|
||||
let showDeleteModal = false;
|
||||
let showRoleModal = false;
|
||||
let showTotpModal = false;
|
||||
let showRoleTotpModal = false;
|
||||
let showDeleteTotpModal = false;
|
||||
let modalError = '';
|
||||
let totpEnabled = false;
|
||||
let pendingRoles: number[] | null = null;
|
||||
let roleTotpCode = '';
|
||||
let deleteTotpCode = '';
|
||||
let now = new Date();
|
||||
let interval: ReturnType<typeof setInterval> | null = null;
|
||||
|
||||
$: isOwner = data.user && $user ? data.user?.uuid === $user.uuid : false;
|
||||
$: totpEnabled = Boolean($user?.totpEnabled);
|
||||
|
||||
const roleOptions = [
|
||||
{ label: 'Member', value: 0 },
|
||||
@@ -69,7 +78,16 @@
|
||||
|
||||
async function handleUserUpdate(displayName?: string, selectedRoles?: number[]) {
|
||||
loading = true;
|
||||
modalError = '';
|
||||
const token = getToken();
|
||||
const currentRoles = data.user?.roles ?? [];
|
||||
const nextRoles = selectedRoles ?? currentRoles;
|
||||
if (needsAdminTotp(totpEnabled, currentRoles, nextRoles) && !roleTotpCode) {
|
||||
pendingRoles = nextRoles;
|
||||
showRoleTotpModal = true;
|
||||
loading = false;
|
||||
return;
|
||||
}
|
||||
const res = await fetch(`/api/user/uuid/${data.user?.uuid}`, {
|
||||
method: 'PATCH',
|
||||
headers: {
|
||||
@@ -78,7 +96,8 @@
|
||||
},
|
||||
body: JSON.stringify({
|
||||
...(displayName !== undefined ? { displayName: displayName.trim() } : {}),
|
||||
...(selectedRoles !== undefined ? { roles: selectedRoles } : {})
|
||||
...(selectedRoles !== undefined ? { roles: selectedRoles } : {}),
|
||||
...(roleTotpCode ? { totpcode: roleTotpCode } : {})
|
||||
})
|
||||
});
|
||||
loading = false;
|
||||
@@ -93,17 +112,36 @@
|
||||
if (selectedRoles !== undefined) {
|
||||
data.user!.roles = selectedRoles;
|
||||
}
|
||||
roleTotpCode = '';
|
||||
showRoleTotpModal = false;
|
||||
pendingRoles = null;
|
||||
modalError = '';
|
||||
}
|
||||
|
||||
function handleRoleTotpConfirm(value: unknown) {
|
||||
if (typeof value === 'string') {
|
||||
roleTotpCode = value.trim();
|
||||
}
|
||||
showRoleTotpModal = false;
|
||||
if (!roleTotpCode) return;
|
||||
handleUserUpdate(undefined, pendingRoles ?? data.user?.roles ?? []);
|
||||
}
|
||||
|
||||
async function handleDeleteAccount() {
|
||||
loading = true;
|
||||
modalError = '';
|
||||
const token = getToken();
|
||||
if (totpEnabled && !deleteTotpCode) {
|
||||
showDeleteTotpModal = true;
|
||||
loading = false;
|
||||
return;
|
||||
}
|
||||
const res = await fetch(`/api/user/uuid/${data.user?.uuid}`, {
|
||||
method: 'DELETE',
|
||||
headers: {
|
||||
Authorization: `Bearer ${token}`
|
||||
}
|
||||
},
|
||||
body: totpEnabled && deleteTotpCode ? JSON.stringify({ totpcode: deleteTotpCode }) : undefined
|
||||
});
|
||||
loading = false;
|
||||
if (!res.ok) {
|
||||
@@ -112,9 +150,29 @@
|
||||
return;
|
||||
}
|
||||
showDeleteModal = false;
|
||||
showDeleteTotpModal = false;
|
||||
deleteTotpCode = '';
|
||||
window.location.href = '/';
|
||||
}
|
||||
|
||||
function handleTotpConfirm(value: unknown) {
|
||||
if (value && typeof value === 'object' && 'enabled' in value) {
|
||||
totpEnabled = Boolean((value as { enabled?: boolean }).enabled);
|
||||
}
|
||||
user.update((u) => (u ? { ...u, totpEnabled } : u));
|
||||
showTotpModal = false;
|
||||
modalError = '';
|
||||
}
|
||||
|
||||
function handleDeleteTotpConfirm(value: unknown) {
|
||||
if (typeof value === 'string') {
|
||||
deleteTotpCode = value.trim();
|
||||
}
|
||||
showDeleteTotpModal = false;
|
||||
if (!deleteTotpCode) return;
|
||||
handleDeleteAccount();
|
||||
}
|
||||
|
||||
onMount(() => {
|
||||
interval = setInterval(() => {
|
||||
now = new Date();
|
||||
@@ -201,6 +259,10 @@
|
||||
{data.user?.uid}
|
||||
</span>
|
||||
</div>
|
||||
<div class="flex shrink-0 items-center">
|
||||
<ShieldUser class="mr-2 h-6 w-6 text-neutral-400" />
|
||||
<span class="text-neutral-400">2FA {totpEnabled ? 'enabled' : 'disabled'}</span>
|
||||
</div>
|
||||
{#if isOwner && data.user?.lastLogin}
|
||||
<div class="flex shrink-0 items-center">
|
||||
<Clock class="mr-2 h-6 w-6 text-neutral-400" />
|
||||
@@ -225,6 +287,18 @@
|
||||
<Pencil class="h-4 w-4" />
|
||||
Edit Display Name
|
||||
</button>
|
||||
{#if isOwner}
|
||||
<button
|
||||
on:click={() => {
|
||||
showTotpModal = true;
|
||||
modalError = '';
|
||||
}}
|
||||
class="flex items-center gap-2 rounded bg-neutral-700 px-4 py-2 text-sm font-medium transition-colors hover:bg-neutral-600"
|
||||
>
|
||||
<ShieldUser class="h-4 w-4" />
|
||||
{totpEnabled ? 'Manage 2FA' : 'Enable 2FA'}
|
||||
</button>
|
||||
{/if}
|
||||
{#if $user?.roles.some((r) => r === 255)}
|
||||
<button
|
||||
on:click={() => {
|
||||
@@ -323,6 +397,22 @@
|
||||
}}
|
||||
/>
|
||||
|
||||
<Modal
|
||||
show={showDeleteTotpModal}
|
||||
mode="totp"
|
||||
title="Enter TOTP to delete"
|
||||
placeholder="Enter 6-digit code"
|
||||
inputType="text"
|
||||
error={modalError}
|
||||
confirmButtonText="Delete Account"
|
||||
onConfirm={(value) => handleDeleteTotpConfirm(value)}
|
||||
onCancel={() => {
|
||||
showDeleteTotpModal = false;
|
||||
deleteTotpCode = '';
|
||||
modalError = '';
|
||||
}}
|
||||
/>
|
||||
|
||||
<Modal
|
||||
show={showRoleModal}
|
||||
mode="multiselect"
|
||||
@@ -337,6 +427,38 @@
|
||||
}}
|
||||
/>
|
||||
|
||||
<Modal
|
||||
show={showTotpModal}
|
||||
mode="totpSetup"
|
||||
title="Two-Factor Authentication"
|
||||
message={totpEnabled
|
||||
? 'Two-factor authentication is currently enabled.'
|
||||
: 'Enable two-factor authentication with an authenticator app.'}
|
||||
error={modalError}
|
||||
totpActive={totpEnabled}
|
||||
onConfirm={(value) => handleTotpConfirm(value)}
|
||||
onCancel={() => {
|
||||
showTotpModal = false;
|
||||
modalError = '';
|
||||
}}
|
||||
/>
|
||||
<Modal
|
||||
show={showRoleTotpModal}
|
||||
mode="totp"
|
||||
title="Admin role requires TOTP"
|
||||
placeholder="Enter 6-digit code"
|
||||
inputType="text"
|
||||
error={modalError}
|
||||
confirmButtonText="Continue"
|
||||
onConfirm={(value) => handleRoleTotpConfirm(value)}
|
||||
onCancel={() => {
|
||||
showRoleTotpModal = false;
|
||||
roleTotpCode = '';
|
||||
pendingRoles = null;
|
||||
modalError = '';
|
||||
}}
|
||||
/>
|
||||
|
||||
<style>
|
||||
button {
|
||||
cursor: pointer;
|
||||
|
||||
Reference in New Issue
Block a user