mirror of
https://github.com/whoswhip/Sharpbin.git
synced 2026-12-05 18:48:41 +00:00
feat: password resets
fix: frontend docker file chore: format via csharpier again
This commit is contained in:
@@ -14,7 +14,11 @@ ENV NODE_ENV=production
|
||||
ENV HOST=0.0.0.0
|
||||
ENV PORT=5173
|
||||
COPY package.json package-lock.json ./
|
||||
RUN npm ci --omit=dev --ignore-scripts && apk add --no-cache wget
|
||||
RUN apk add --no-cache wget libstdc++ \
|
||||
&& apk add --no-cache --virtual .build-deps python3 make g++ \
|
||||
&& npm ci --omit=dev \
|
||||
&& npm cache clean --force \
|
||||
&& apk del .build-deps
|
||||
COPY --from=build --chown=node:node /app/build ./build
|
||||
RUN chown -R node:node /app
|
||||
EXPOSE 5173
|
||||
|
||||
@@ -7,6 +7,7 @@ export interface User {
|
||||
username: string;
|
||||
displayName: string | null;
|
||||
email: string | null;
|
||||
emailVerified: boolean;
|
||||
lastLogin: number | null;
|
||||
roles: number; // bitfield; 5 = User | Admin
|
||||
isBanned: boolean;
|
||||
|
||||
@@ -98,7 +98,7 @@
|
||||
|
||||
<svelte:head>
|
||||
<link rel="icon" href={favicon} />
|
||||
{#if data.options?.cf_turnstile_site_key && (page.url.pathname === '/login' || page.url.pathname === '/register' || page.url.pathname === '/')}
|
||||
{#if data.options?.cf_turnstile_site_key && (page.url.pathname === '/login' || page.url.pathname === '/register' || page.url.pathname === '/' || page.url.pathname === '/forgot-password')}
|
||||
<script
|
||||
src="https://challenges.cloudflare.com/turnstile/v0/api.js?render=explicit&onload=turnstileLoaded"
|
||||
async
|
||||
|
||||
@@ -0,0 +1,123 @@
|
||||
<script lang="ts">
|
||||
import { resolve } from '$app/paths';
|
||||
import { extractError } from '$lib/utils/misc';
|
||||
import { onMount } from 'svelte';
|
||||
import type { PageData } from './$types';
|
||||
|
||||
let email = $state('');
|
||||
let error = $state('');
|
||||
let message = $state('');
|
||||
|
||||
interface Props {
|
||||
data: PageData;
|
||||
}
|
||||
|
||||
let { data }: Props = $props();
|
||||
|
||||
onMount(() => {
|
||||
const render = () => {
|
||||
if (window.turnstile && data.options?.cf_turnstile_site_key) {
|
||||
window.turnstile.render('.cf-turnstile', {
|
||||
sitekey: data.options.cf_turnstile_site_key,
|
||||
theme: 'dark',
|
||||
size: 'flexible',
|
||||
appearence: 'interaction-only'
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
if (window.turnstile) {
|
||||
render();
|
||||
} else {
|
||||
window.addEventListener('turnstile:loaded', render, { once: true });
|
||||
}
|
||||
|
||||
return () => {
|
||||
window.removeEventListener('turnstile:loaded', render);
|
||||
};
|
||||
});
|
||||
|
||||
async function submit(event: Event) {
|
||||
event.preventDefault();
|
||||
error = '';
|
||||
message = '';
|
||||
|
||||
const body: { email: string; token?: string } = { email };
|
||||
if (data.options?.cf_turnstile_site_key && window.turnstile) {
|
||||
const token = window.turnstile.getResponse();
|
||||
if (token) body.token = token;
|
||||
}
|
||||
|
||||
const res = await fetch('/api/auth/password/forgot', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(body)
|
||||
});
|
||||
|
||||
const json = await res.json().catch(() => ({}));
|
||||
if (!res.ok) {
|
||||
if (window.turnstile) window.turnstile.reset();
|
||||
error = extractError(json) || 'Request failed.';
|
||||
return;
|
||||
}
|
||||
|
||||
if (window.turnstile) window.turnstile.reset();
|
||||
message = json.message || 'If an account exists for that email, a reset link has been sent.';
|
||||
email = '';
|
||||
}
|
||||
</script>
|
||||
|
||||
<svelte:head>
|
||||
<title>Forgot Password - Sharpbin</title>
|
||||
<meta name="description" content="Request a password reset link for your Sharpbin account." />
|
||||
</svelte:head>
|
||||
|
||||
<main
|
||||
class="flex min-h-[calc(100vh-120px)] w-full flex-col items-center justify-center pt-5 pb-5 text-white"
|
||||
>
|
||||
<div class="w-full max-w-md rounded border-2 border-neutral-800 bg-neutral-900 p-6">
|
||||
<h1 class="mb-2 text-center text-3xl font-bold">Forgot Password</h1>
|
||||
<p class="mb-6 text-center text-sm text-neutral-400">
|
||||
Enter the email address on your account and we’ll send a reset link if one exists.
|
||||
</p>
|
||||
<form onsubmit={submit} class="space-y-4">
|
||||
<input
|
||||
type="email"
|
||||
placeholder="Email"
|
||||
bind:value={email}
|
||||
autocomplete="email"
|
||||
required
|
||||
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 data.options?.cf_turnstile_site_key}
|
||||
<div class="cf-turnstile w-full"></div>
|
||||
{/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"
|
||||
>
|
||||
Send Reset Link
|
||||
</button>
|
||||
{#if error}
|
||||
<div
|
||||
class="rounded border border-red-900 bg-red-950 p-2 text-sm whitespace-pre-line text-red-200"
|
||||
>
|
||||
{error}
|
||||
</div>
|
||||
{/if}
|
||||
{#if message}
|
||||
<div
|
||||
class="rounded border border-green-900 bg-green-950 p-2 text-sm whitespace-pre-line text-green-200"
|
||||
>
|
||||
{message}
|
||||
</div>
|
||||
{/if}
|
||||
</form>
|
||||
<p class="mt-4 text-center text-sm text-neutral-400">
|
||||
<a
|
||||
href={resolve('/login')}
|
||||
class="text-neutral-200 transition-colors duration-200 hover:text-white">Back to login</a
|
||||
>
|
||||
</p>
|
||||
</div>
|
||||
</main>
|
||||
@@ -159,5 +159,11 @@
|
||||
class="text-neutral-200 transition-colors duration-200 hover:text-white">Register</a
|
||||
>
|
||||
</p>
|
||||
<p class="mt-2 text-center text-sm text-neutral-400">
|
||||
<a
|
||||
href={resolve('/forgot-password')}
|
||||
class="text-neutral-200 transition-colors duration-200 hover:text-white">Forgot password?</a
|
||||
>
|
||||
</p>
|
||||
</div>
|
||||
</main>
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
import { apiUrl } from '$lib/server/api';
|
||||
import { extractError } from '$lib/utils/misc';
|
||||
import { error } from '@sveltejs/kit';
|
||||
import type { PageServerLoad } from './$types';
|
||||
|
||||
export const load: PageServerLoad = async ({ url, request }) => {
|
||||
const token = url.searchParams.get('token');
|
||||
if (!token) {
|
||||
throw error(400, 'Reset token is required.');
|
||||
}
|
||||
|
||||
const headers = new Headers(request.headers);
|
||||
const res = await fetch(`${apiUrl}/api/auth/password/reset-info?token=${token}`, { headers });
|
||||
const json = await res.json().catch(() => null);
|
||||
if (!res.ok) {
|
||||
const message = extractError(json);
|
||||
throw error(res.status, message ?? 'Request failed');
|
||||
}
|
||||
|
||||
return {
|
||||
token,
|
||||
username: typeof json?.username === 'string' ? json.username : undefined,
|
||||
requiresTotp: Boolean(json?.requiresTotp)
|
||||
};
|
||||
};
|
||||
@@ -0,0 +1,231 @@
|
||||
<script lang="ts">
|
||||
import { resolve } from '$app/paths';
|
||||
import { extractError } from '$lib/utils/misc';
|
||||
import { Check, X } from '@lucide/svelte';
|
||||
import { slide, fade } from 'svelte/transition';
|
||||
import type { PageData } from './$types';
|
||||
|
||||
interface Props {
|
||||
data: PageData;
|
||||
}
|
||||
|
||||
let { data }: Props = $props();
|
||||
let password = $state('');
|
||||
let confirmPassword = $state('');
|
||||
let totpCode = $state('');
|
||||
let error = $state('');
|
||||
let message = $state('');
|
||||
let complete = $state(false);
|
||||
let loading = $state(false);
|
||||
let passwordFocused = $state(false);
|
||||
let passwordChecks = $state({
|
||||
minLength: false,
|
||||
maxLength: false,
|
||||
upper: false,
|
||||
lower: false,
|
||||
number: false
|
||||
});
|
||||
let passwordValid = $state(false);
|
||||
|
||||
function validatePassword(pw: string) {
|
||||
passwordChecks.minLength = pw.length >= 8;
|
||||
passwordChecks.maxLength = pw.length <= 128;
|
||||
passwordChecks.upper = /[A-Z]/.test(pw);
|
||||
passwordChecks.lower = /[a-z]/.test(pw);
|
||||
passwordChecks.number = /[0-9]/.test(pw);
|
||||
passwordValid =
|
||||
passwordChecks.minLength &&
|
||||
passwordChecks.maxLength &&
|
||||
passwordChecks.upper &&
|
||||
passwordChecks.lower &&
|
||||
passwordChecks.number;
|
||||
}
|
||||
|
||||
$effect(() => {
|
||||
validatePassword(password);
|
||||
});
|
||||
|
||||
async function submit(event: Event) {
|
||||
event.preventDefault();
|
||||
error = '';
|
||||
message = '';
|
||||
|
||||
if (!password || !confirmPassword) {
|
||||
error = 'Both password fields are required.';
|
||||
return;
|
||||
}
|
||||
if (password !== confirmPassword) {
|
||||
error = 'Passwords do not match.';
|
||||
return;
|
||||
}
|
||||
if (!passwordValid) {
|
||||
error =
|
||||
'Password should have at least 8 characters, no more than 128 characters, including uppercase, lowercase, and digits.';
|
||||
return;
|
||||
}
|
||||
|
||||
loading = true;
|
||||
try {
|
||||
const res = await fetch('/api/auth/password/reset', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
token: data.token,
|
||||
newPassword: password,
|
||||
...(data.requiresTotp ? { totpCode } : {})
|
||||
})
|
||||
});
|
||||
const json = await res.json().catch(() => ({}));
|
||||
if (!res.ok) {
|
||||
error = extractError(json) || 'Password reset failed.';
|
||||
return;
|
||||
}
|
||||
complete = true;
|
||||
message = json.message || 'Password reset successfully.';
|
||||
} finally {
|
||||
loading = false;
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<svelte:head>
|
||||
<title>Reset Password - Sharpbin</title>
|
||||
<meta name="description" content="Set a new password for your Sharpbin account." />
|
||||
</svelte:head>
|
||||
|
||||
<main
|
||||
class="flex min-h-[calc(100vh-120px)] w-full flex-col items-center justify-center pt-5 pb-5 text-white"
|
||||
>
|
||||
<div class="w-full max-w-md rounded border-2 border-neutral-800 bg-neutral-900 p-6">
|
||||
{#if complete}
|
||||
<h1 class="mb-2 text-center text-3xl font-bold">Password Reset</h1>
|
||||
<p class="text-center text-sm text-neutral-400">{message}</p>
|
||||
<p class="mt-4 text-center text-sm text-neutral-400">
|
||||
<a
|
||||
href={resolve('/login')}
|
||||
class="text-neutral-200 transition-colors duration-200 hover:text-white">Back to login</a
|
||||
>
|
||||
</p>
|
||||
{:else}
|
||||
<h1 class="mb-2 text-center text-3xl font-bold">Reset Password</h1>
|
||||
<p class="mb-6 text-center text-sm text-neutral-400">
|
||||
{#if data.username}
|
||||
Reset the password for {data.username}.
|
||||
{:else}
|
||||
Set a new password for your account.
|
||||
{/if}
|
||||
</p>
|
||||
<form onsubmit={submit} class="space-y-4">
|
||||
<input
|
||||
type="password"
|
||||
placeholder="New password"
|
||||
bind:value={password}
|
||||
autocomplete="new-password"
|
||||
required
|
||||
onfocus={() => (passwordFocused = true)}
|
||||
onblur={() => (passwordFocused = false)}
|
||||
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 passwordFocused}
|
||||
<ul
|
||||
transition:slide
|
||||
class="space-y-2 rounded border border-neutral-700 bg-neutral-800 p-3 text-sm"
|
||||
>
|
||||
<li
|
||||
class="flex items-center gap-2 {passwordChecks.minLength
|
||||
? 'text-green-400'
|
||||
: 'text-red-400'}"
|
||||
>
|
||||
{#if passwordChecks.minLength}<Check class="h-4 w-4" />{:else}<X
|
||||
class="h-4 w-4"
|
||||
/>{/if}
|
||||
At least 8 characters
|
||||
</li>
|
||||
<li
|
||||
class="flex items-center gap-2 {passwordChecks.maxLength
|
||||
? 'text-green-400'
|
||||
: 'text-red-400'}"
|
||||
>
|
||||
{#if passwordChecks.maxLength}<Check class="h-4 w-4" />{:else}<X
|
||||
class="h-4 w-4"
|
||||
/>{/if}
|
||||
No more than 128 characters
|
||||
</li>
|
||||
<li
|
||||
class="flex items-center gap-2 {passwordChecks.upper
|
||||
? 'text-green-400'
|
||||
: 'text-red-400'}"
|
||||
>
|
||||
{#if passwordChecks.upper}<Check class="h-4 w-4" />{:else}<X class="h-4 w-4" />{/if}
|
||||
At least one uppercase letter
|
||||
</li>
|
||||
<li
|
||||
class="flex items-center gap-2 {passwordChecks.lower
|
||||
? 'text-green-400'
|
||||
: 'text-red-400'}"
|
||||
>
|
||||
{#if passwordChecks.lower}<Check class="h-4 w-4" />{:else}<X class="h-4 w-4" />{/if}
|
||||
At least one lowercase letter
|
||||
</li>
|
||||
<li
|
||||
class="flex items-center gap-2 {passwordChecks.number
|
||||
? 'text-green-400'
|
||||
: 'text-red-400'}"
|
||||
>
|
||||
{#if passwordChecks.number}<Check class="h-4 w-4" />{:else}<X class="h-4 w-4" />{/if}
|
||||
At least one number
|
||||
</li>
|
||||
</ul>
|
||||
{/if}
|
||||
<input
|
||||
type="password"
|
||||
placeholder="Confirm new password"
|
||||
bind:value={confirmPassword}
|
||||
autocomplete="new-password"
|
||||
required
|
||||
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 confirmPassword && password !== confirmPassword}
|
||||
<div
|
||||
transition:fade
|
||||
class="rounded border border-red-900 bg-red-950 p-2 text-sm text-red-200"
|
||||
>
|
||||
Passwords do not match.
|
||||
</div>
|
||||
{/if}
|
||||
{#if data.requiresTotp}
|
||||
<input
|
||||
type="text"
|
||||
placeholder="TOTP Code"
|
||||
bind:value={totpCode}
|
||||
maxlength="6"
|
||||
inputmode="numeric"
|
||||
pattern="[0-9]*"
|
||||
required
|
||||
oninput={(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}
|
||||
<button
|
||||
type="submit"
|
||||
disabled={loading || !passwordValid || password !== confirmPassword}
|
||||
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:opacity-50"
|
||||
>
|
||||
{loading ? 'Updating...' : 'Reset Password'}
|
||||
</button>
|
||||
{#if error}
|
||||
<div
|
||||
class="rounded border border-red-900 bg-red-950 p-2 text-sm whitespace-pre-line text-red-200"
|
||||
>
|
||||
{error}
|
||||
</div>
|
||||
{/if}
|
||||
</form>
|
||||
{/if}
|
||||
</div>
|
||||
</main>
|
||||
@@ -14,6 +14,7 @@
|
||||
Ban,
|
||||
AtSign,
|
||||
Pencil,
|
||||
KeyRound,
|
||||
Trash2,
|
||||
Clock,
|
||||
Hash,
|
||||
@@ -30,7 +31,7 @@
|
||||
dateToRelativeString,
|
||||
extractError
|
||||
} from '$lib/utils/misc';
|
||||
import { getToken, hasRole, roles } from '$lib/utils/auth';
|
||||
import { clearTokens, getToken, hasRole, roles } from '$lib/utils/auth';
|
||||
import { openModal } from '$lib/stores/modal';
|
||||
import { user } from '$lib/stores/user';
|
||||
import { onMount } from 'svelte';
|
||||
@@ -64,6 +65,30 @@
|
||||
let interval: ReturnType<typeof setInterval> | null = null;
|
||||
let isEditingDisplayName = $state(false);
|
||||
let editDisplayNameValue = $state('');
|
||||
let isEditingEmail = $state(false);
|
||||
let editEmailValue = $state('');
|
||||
let emailActionLoading = $state(false);
|
||||
let emailMessage = $state('');
|
||||
let emailError = $state('');
|
||||
let passwordCurrentValue = $state('');
|
||||
let passwordNewValue = $state('');
|
||||
let passwordConfirmValue = $state('');
|
||||
let passwordFocused = $state(false);
|
||||
let passwordChecks = $state({
|
||||
minLength: false,
|
||||
maxLength: false,
|
||||
upper: false,
|
||||
lower: false,
|
||||
number: false
|
||||
});
|
||||
let passwordValid = $state(false);
|
||||
let passwordActionLoading = $state(false);
|
||||
let passwordMessage = $state('');
|
||||
let passwordError = $state('');
|
||||
let passwordResetLinkValue = $state('');
|
||||
let passwordResetLinkLoading = $state(false);
|
||||
let passwordResetLinkMessage = $state('');
|
||||
let passwordResetLinkError = $state('');
|
||||
|
||||
let apiKeys: { uuid: string; name: string; createdAt: string; lastUsedAt: string | null }[] =
|
||||
$state([]);
|
||||
@@ -76,6 +101,12 @@
|
||||
|
||||
let reportsSubmitted = $derived(data.reportsSubmitted);
|
||||
let reportsTarget = $derived(data.reportsTarget);
|
||||
let adminsRequire2FA = $derived(
|
||||
Boolean(
|
||||
(data as unknown as { authOptions?: { admins_require_2fa?: boolean } }).authOptions
|
||||
?.admins_require_2fa
|
||||
)
|
||||
);
|
||||
|
||||
let activeTab: 'pastes' | 'reportsSubmitted' | 'reportsTarget' | 'settings' = $state('pastes');
|
||||
let activeSubTab:
|
||||
@@ -184,7 +215,7 @@
|
||||
message?: string;
|
||||
};
|
||||
|
||||
type SecurityAction = 'UpdateRoles' | 'DeleteUser';
|
||||
type SecurityAction = 'UpdateRoles' | 'DeleteUser' | 'GeneratePasswordResetLink';
|
||||
|
||||
async function fetchActionSecurityRequirements(action: SecurityAction) {
|
||||
const token = getToken();
|
||||
@@ -248,6 +279,24 @@
|
||||
).trim();
|
||||
}
|
||||
|
||||
function validatePassword(pw: string) {
|
||||
passwordChecks.minLength = pw.length >= 8;
|
||||
passwordChecks.maxLength = pw.length <= 128;
|
||||
passwordChecks.upper = /[A-Z]/.test(pw);
|
||||
passwordChecks.lower = /[a-z]/.test(pw);
|
||||
passwordChecks.number = /[0-9]/.test(pw);
|
||||
passwordValid =
|
||||
passwordChecks.minLength &&
|
||||
passwordChecks.maxLength &&
|
||||
passwordChecks.upper &&
|
||||
passwordChecks.lower &&
|
||||
passwordChecks.number;
|
||||
}
|
||||
|
||||
$effect(() => {
|
||||
validatePassword(passwordNewValue);
|
||||
});
|
||||
|
||||
async function fetchPage(pageNum: number) {
|
||||
if (pageNum < 1 || pageNum > (pagination.totalPages || 1) || loading) return;
|
||||
loading = true;
|
||||
@@ -305,11 +354,182 @@
|
||||
modalError = '';
|
||||
}
|
||||
|
||||
async function changeEmail() {
|
||||
const email = editEmailValue.trim();
|
||||
if (!email) return;
|
||||
let totpcode = '';
|
||||
if (totpEnabled) {
|
||||
totpcode = await promptTotpCode('TOTP required to change email', 'Continue');
|
||||
if (!totpcode) return;
|
||||
}
|
||||
emailActionLoading = true;
|
||||
emailMessage = '';
|
||||
emailError = '';
|
||||
const token = getToken();
|
||||
try {
|
||||
const res = await fetch('/api/auth/email/change', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${token}`
|
||||
},
|
||||
body: JSON.stringify({
|
||||
email,
|
||||
...(totpcode ? { totpcode } : {})
|
||||
})
|
||||
});
|
||||
const json = await res.json().catch(() => ({}));
|
||||
if (!res.ok) {
|
||||
emailError = extractError(json) || 'Failed to send verification email.';
|
||||
return;
|
||||
}
|
||||
emailMessage = json.message || 'Verification email sent.';
|
||||
isEditingEmail = false;
|
||||
editEmailValue = '';
|
||||
} catch {
|
||||
emailError = 'Network error';
|
||||
} finally {
|
||||
emailActionLoading = false;
|
||||
}
|
||||
}
|
||||
|
||||
async function changePassword() {
|
||||
passwordMessage = '';
|
||||
passwordError = '';
|
||||
const currentPassword = passwordCurrentValue;
|
||||
const newPassword = passwordNewValue;
|
||||
const confirmPassword = passwordConfirmValue;
|
||||
if (!currentPassword || !newPassword || !confirmPassword) {
|
||||
passwordError = 'Current password, new password, and confirmation are required.';
|
||||
return;
|
||||
}
|
||||
if (newPassword !== confirmPassword) {
|
||||
passwordError = 'New passwords do not match.';
|
||||
return;
|
||||
}
|
||||
if (!passwordValid) {
|
||||
passwordError =
|
||||
'Password should have at least 8 characters, no more than 128 characters, including uppercase, lowercase, and digits.';
|
||||
return;
|
||||
}
|
||||
let totpcode = '';
|
||||
if (totpEnabled || adminsRequire2FA) {
|
||||
if (adminsRequire2FA && !totpEnabled) {
|
||||
passwordError = '2FA is required to perform this action.';
|
||||
return;
|
||||
}
|
||||
totpcode = await promptTotpCode('TOTP required to change password', 'Continue');
|
||||
if (!totpcode) return;
|
||||
}
|
||||
passwordActionLoading = true;
|
||||
const token = getToken();
|
||||
try {
|
||||
const res = await fetch('/api/auth/password/change', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${token}`
|
||||
},
|
||||
body: JSON.stringify({
|
||||
currentPassword,
|
||||
newPassword,
|
||||
...(totpcode ? { totpCode: totpcode } : {})
|
||||
})
|
||||
});
|
||||
const json = await res.json().catch(() => ({}));
|
||||
if (!res.ok) {
|
||||
passwordError = extractError(json) || 'Failed to change password.';
|
||||
return;
|
||||
}
|
||||
clearTokens();
|
||||
window.location.href = '/login';
|
||||
} catch {
|
||||
passwordError = 'Network error';
|
||||
} finally {
|
||||
passwordActionLoading = false;
|
||||
}
|
||||
}
|
||||
|
||||
async function generatePasswordResetLink() {
|
||||
passwordResetLinkMessage = '';
|
||||
passwordResetLinkError = '';
|
||||
const token = getToken();
|
||||
if (!token || !data.user?.uuid) return;
|
||||
|
||||
try {
|
||||
const requirements = await fetchActionSecurityRequirements('GeneratePasswordResetLink');
|
||||
if (requirements.blocked) {
|
||||
passwordResetLinkError =
|
||||
requirements.message || 'You cannot perform this action right now.';
|
||||
return;
|
||||
}
|
||||
let totpcode = '';
|
||||
if (requirements.requiresTotp) {
|
||||
totpcode = await promptTotpCode('TOTP required to generate reset link', 'Continue');
|
||||
if (!totpcode) return;
|
||||
}
|
||||
|
||||
passwordResetLinkLoading = true;
|
||||
const res = await fetch(`/api/auth/password/admin-reset-link/${data.user.uuid}`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${token}`
|
||||
},
|
||||
body: JSON.stringify({
|
||||
...(totpcode ? { totpCode: totpcode } : {})
|
||||
})
|
||||
});
|
||||
const json = await res.json().catch(() => ({}));
|
||||
if (!res.ok) {
|
||||
passwordResetLinkError = extractError(json) || 'Failed to generate password reset link.';
|
||||
return;
|
||||
}
|
||||
passwordResetLinkValue = json.resetLink || '';
|
||||
passwordResetLinkMessage = json.message || 'Password reset link generated.';
|
||||
} catch {
|
||||
passwordResetLinkError = 'Network error';
|
||||
} finally {
|
||||
passwordResetLinkLoading = false;
|
||||
}
|
||||
}
|
||||
|
||||
async function resendVerificationEmail() {
|
||||
emailActionLoading = true;
|
||||
emailMessage = '';
|
||||
emailError = '';
|
||||
const token = getToken();
|
||||
try {
|
||||
const res = await fetch('/api/auth/email/resend-verification', {
|
||||
method: 'POST',
|
||||
headers: { Authorization: `Bearer ${token}` }
|
||||
});
|
||||
const json = await res.json().catch(() => ({}));
|
||||
if (!res.ok) {
|
||||
emailError = extractError(json) || 'Failed to resend verification email.';
|
||||
return;
|
||||
}
|
||||
emailMessage = json.message || 'Verification email sent.';
|
||||
} catch {
|
||||
emailError = 'Network error';
|
||||
} finally {
|
||||
emailActionLoading = false;
|
||||
}
|
||||
}
|
||||
|
||||
function startEditEmail() {
|
||||
editEmailValue = data.user?.email || '';
|
||||
isEditingEmail = true;
|
||||
emailMessage = '';
|
||||
emailError = '';
|
||||
}
|
||||
|
||||
async function handleDeleteAccount() {
|
||||
loading = true;
|
||||
modalError = '';
|
||||
const token = getToken();
|
||||
let totpcode = '';
|
||||
let verificationToken = '';
|
||||
if (totpEnabled) {
|
||||
totpcode = String(
|
||||
await openModal<string>({
|
||||
@@ -325,6 +545,42 @@
|
||||
loading = false;
|
||||
return;
|
||||
}
|
||||
} else if (isOwner && data.user?.emailVerified && data.user?.email) {
|
||||
const requestRes = await fetch(`/api/user/uuid/${data.user?.uuid}/delete-verification`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
Authorization: `Bearer ${token}`
|
||||
}
|
||||
});
|
||||
if (!requestRes.ok) {
|
||||
const err = await requestRes.json().catch(() => ({}));
|
||||
loading = false;
|
||||
modalError = extractError(err) || 'Failed to send account deletion verification email.';
|
||||
await openModal({
|
||||
mode: 'confirm',
|
||||
title: 'Error',
|
||||
message: modalError,
|
||||
confirmButtonText: 'OK',
|
||||
cancelValue: true
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
verificationToken = String(
|
||||
await openModal<string>({
|
||||
mode: 'prompt',
|
||||
title: 'Verify Account Deletion',
|
||||
message: 'Enter the account deletion code sent to your verified email.',
|
||||
placeholder: 'Deletion code',
|
||||
inputType: 'text',
|
||||
confirmButtonText: 'Delete Account',
|
||||
cancelValue: ''
|
||||
})
|
||||
).trim();
|
||||
if (!verificationToken) {
|
||||
loading = false;
|
||||
return;
|
||||
}
|
||||
}
|
||||
const res = await fetch(`/api/user/uuid/${data.user?.uuid}`, {
|
||||
method: 'DELETE',
|
||||
@@ -332,7 +588,12 @@
|
||||
Authorization: `Bearer ${token}`,
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: totpEnabled && totpcode ? JSON.stringify({ totpcode }) : undefined
|
||||
body:
|
||||
totpEnabled && totpcode
|
||||
? JSON.stringify({ totpcode })
|
||||
: verificationToken
|
||||
? JSON.stringify({ token: verificationToken })
|
||||
: undefined
|
||||
});
|
||||
loading = false;
|
||||
if (!res.ok) {
|
||||
@@ -347,6 +608,10 @@
|
||||
});
|
||||
return;
|
||||
}
|
||||
if (isOwner) {
|
||||
clearTokens();
|
||||
user.set(null);
|
||||
}
|
||||
window.location.href = '/';
|
||||
}
|
||||
|
||||
@@ -847,6 +1112,117 @@
|
||||
{/if}
|
||||
</div>
|
||||
</div>
|
||||
{#if isOwner}
|
||||
<div class="col-span-2">
|
||||
<h2 class="text-xl font-semibold text-neutral-100">Email</h2>
|
||||
<p class="mb-1 text-sm text-neutral-400">
|
||||
Used for account verification and security notifications.
|
||||
</p>
|
||||
|
||||
{#if emailError}
|
||||
<div
|
||||
class="mb-3 rounded border border-red-900/50 bg-red-900/10 p-3 text-sm text-red-200"
|
||||
>
|
||||
{emailError}
|
||||
</div>
|
||||
{/if}
|
||||
{#if emailMessage}
|
||||
<div
|
||||
class="mb-3 rounded border border-green-900/50 bg-green-900/10 p-3 text-sm text-green-200"
|
||||
>
|
||||
{emailMessage}
|
||||
</div>
|
||||
{/if}
|
||||
|
||||
<div class="rounded bg-neutral-800/50 p-3">
|
||||
{#if isEditingEmail}
|
||||
<div class="flex flex-col gap-3 sm:flex-row sm:items-center">
|
||||
<input
|
||||
type="email"
|
||||
bind:value={editEmailValue}
|
||||
placeholder="you@example.com"
|
||||
class="w-full border-b border-neutral-600 bg-transparent px-2 py-1 font-mono text-neutral-300 outline-none focus:border-neutral-400"
|
||||
autocomplete="email"
|
||||
onkeydown={(e) => {
|
||||
if (e.key === 'Enter') changeEmail();
|
||||
else if (e.key === 'Escape') {
|
||||
isEditingEmail = false;
|
||||
editEmailValue = '';
|
||||
}
|
||||
}}
|
||||
/>
|
||||
<div class="flex shrink-0 gap-2">
|
||||
<button
|
||||
onclick={changeEmail}
|
||||
disabled={emailActionLoading || !editEmailValue.trim()}
|
||||
class="flex items-center gap-1 rounded bg-green-900/50 px-3 py-2 text-xs font-medium text-green-400 transition-colors hover:bg-green-900 disabled:opacity-50"
|
||||
>
|
||||
{#if emailActionLoading}
|
||||
<LoaderCircle class="h-4 w-4 animate-spin" />
|
||||
{:else}
|
||||
<Check class="h-4 w-4" />
|
||||
{/if}
|
||||
Send verification
|
||||
</button>
|
||||
<button
|
||||
onclick={() => {
|
||||
isEditingEmail = false;
|
||||
editEmailValue = '';
|
||||
}}
|
||||
class="flex items-center gap-1 rounded bg-neutral-700 px-3 py-2 text-xs font-medium text-neutral-300 transition-colors hover:bg-neutral-600"
|
||||
>
|
||||
<X class="h-4 w-4" />
|
||||
Cancel
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
{:else}
|
||||
<div class="flex flex-col gap-3 sm:flex-row sm:items-center sm:justify-between">
|
||||
<div class="flex min-w-0 flex-col gap-1">
|
||||
<span class="font-mono break-all text-neutral-300">
|
||||
{data.user?.email || 'No email set'}
|
||||
</span>
|
||||
<span
|
||||
class="text-sm {data.user?.emailVerified
|
||||
? 'text-green-400'
|
||||
: 'text-amber-400'}"
|
||||
>
|
||||
{data.user?.email
|
||||
? data.user?.emailVerified
|
||||
? 'Verified'
|
||||
: 'Not verified'
|
||||
: 'Add an email to enable verification'}
|
||||
</span>
|
||||
</div>
|
||||
<div class="flex flex-wrap gap-2">
|
||||
{#if data.user?.email && !data.user?.emailVerified}
|
||||
<button
|
||||
onclick={resendVerificationEmail}
|
||||
disabled={emailActionLoading}
|
||||
class="flex items-center gap-2 rounded bg-neutral-700 px-4 py-2 text-sm font-medium transition-colors hover:bg-neutral-600 disabled:opacity-50"
|
||||
>
|
||||
{#if emailActionLoading}
|
||||
<LoaderCircle class="h-4 w-4 animate-spin" />
|
||||
{:else}
|
||||
<Check class="h-4 w-4" />
|
||||
{/if}
|
||||
Resend
|
||||
</button>
|
||||
{/if}
|
||||
<button
|
||||
onclick={startEditEmail}
|
||||
disabled={emailActionLoading}
|
||||
class="flex items-center gap-2 rounded bg-neutral-700 px-4 py-2 text-sm font-medium transition-colors hover:bg-neutral-600 disabled:opacity-50"
|
||||
>
|
||||
<Pencil class="h-4 w-4" />
|
||||
{data.user?.email ? 'Change' : 'Add'}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
{/if}
|
||||
</div>
|
||||
</div>
|
||||
{/if}
|
||||
{:else if isOwner && activeSubTab === 'settings:security'}
|
||||
<div class="col-span-2">
|
||||
<div class="space-y-4">
|
||||
@@ -864,6 +1240,138 @@
|
||||
{totpEnabled ? 'Manage' : 'Enable'}
|
||||
</button>
|
||||
</div>
|
||||
<div class="rounded bg-neutral-800/50 p-4">
|
||||
<div class="mb-3 flex flex-col gap-1">
|
||||
<span class="font-medium text-neutral-200">Change Password</span>
|
||||
<span class="text-sm text-neutral-500">
|
||||
Use your current password and choose a new one.
|
||||
</span>
|
||||
</div>
|
||||
|
||||
{#if passwordError}
|
||||
<div
|
||||
class="mb-3 rounded border border-red-900/50 bg-red-900/10 p-3 text-sm text-red-200"
|
||||
>
|
||||
{passwordError}
|
||||
</div>
|
||||
{/if}
|
||||
{#if passwordMessage}
|
||||
<div
|
||||
class="mb-3 rounded border border-green-900/50 bg-green-900/10 p-3 text-sm text-green-200"
|
||||
>
|
||||
{passwordMessage}
|
||||
</div>
|
||||
{/if}
|
||||
|
||||
<div class="grid gap-3">
|
||||
<input
|
||||
type="password"
|
||||
bind:value={passwordCurrentValue}
|
||||
placeholder="Current password"
|
||||
autocomplete="current-password"
|
||||
class="w-full rounded border border-neutral-700 bg-neutral-900 px-3 py-2 text-neutral-200 transition-colors outline-none focus:border-neutral-500"
|
||||
/>
|
||||
<input
|
||||
type="password"
|
||||
bind:value={passwordNewValue}
|
||||
placeholder="New password"
|
||||
autocomplete="new-password"
|
||||
onfocus={() => (passwordFocused = true)}
|
||||
onblur={() => (passwordFocused = false)}
|
||||
class="w-full rounded border border-neutral-700 bg-neutral-900 px-3 py-2 text-neutral-200 transition-colors outline-none focus:border-neutral-500"
|
||||
/>
|
||||
{#if passwordFocused}
|
||||
<ul
|
||||
transition:fade
|
||||
class="space-y-2 rounded border border-neutral-700 bg-neutral-800 p-3 text-sm"
|
||||
>
|
||||
<li
|
||||
class="flex items-center gap-2 {passwordChecks.minLength
|
||||
? 'text-green-400'
|
||||
: 'text-red-400'}"
|
||||
>
|
||||
{#if passwordChecks.minLength}
|
||||
<Check class="h-4 w-4" />
|
||||
{:else}
|
||||
<X class="h-4 w-4" />
|
||||
{/if}
|
||||
At least 8 characters
|
||||
</li>
|
||||
<li
|
||||
class="flex items-center gap-2 {passwordChecks.maxLength
|
||||
? 'text-green-400'
|
||||
: 'text-red-400'}"
|
||||
>
|
||||
{#if passwordChecks.maxLength}
|
||||
<Check class="h-4 w-4" />
|
||||
{:else}
|
||||
<X class="h-4 w-4" />
|
||||
{/if}
|
||||
No more than 128 characters
|
||||
</li>
|
||||
<li
|
||||
class="flex items-center gap-2 {passwordChecks.upper
|
||||
? 'text-green-400'
|
||||
: 'text-red-400'}"
|
||||
>
|
||||
{#if passwordChecks.upper}
|
||||
<Check class="h-4 w-4" />
|
||||
{:else}
|
||||
<X class="h-4 w-4" />
|
||||
{/if}
|
||||
At least one uppercase letter
|
||||
</li>
|
||||
<li
|
||||
class="flex items-center gap-2 {passwordChecks.lower
|
||||
? 'text-green-400'
|
||||
: 'text-red-400'}"
|
||||
>
|
||||
{#if passwordChecks.lower}
|
||||
<Check class="h-4 w-4" />
|
||||
{:else}
|
||||
<X class="h-4 w-4" />
|
||||
{/if}
|
||||
At least one lowercase letter
|
||||
</li>
|
||||
<li
|
||||
class="flex items-center gap-2 {passwordChecks.number
|
||||
? 'text-green-400'
|
||||
: 'text-red-400'}"
|
||||
>
|
||||
{#if passwordChecks.number}
|
||||
<Check class="h-4 w-4" />
|
||||
{:else}
|
||||
<X class="h-4 w-4" />
|
||||
{/if}
|
||||
At least one number
|
||||
</li>
|
||||
</ul>
|
||||
{/if}
|
||||
<input
|
||||
type="password"
|
||||
bind:value={passwordConfirmValue}
|
||||
placeholder="Confirm new password"
|
||||
autocomplete="new-password"
|
||||
class="w-full rounded border border-neutral-700 bg-neutral-900 px-3 py-2 text-neutral-200 transition-colors outline-none focus:border-neutral-500"
|
||||
/>
|
||||
<div class="flex items-center justify-end">
|
||||
<button
|
||||
onclick={changePassword}
|
||||
disabled={passwordActionLoading ||
|
||||
!passwordValid ||
|
||||
passwordNewValue !== passwordConfirmValue}
|
||||
class="flex items-center gap-2 rounded bg-neutral-700 px-4 py-2 text-sm font-medium transition-colors hover:bg-neutral-600 disabled:opacity-50"
|
||||
>
|
||||
{#if passwordActionLoading}
|
||||
<LoaderCircle class="h-4 w-4 animate-spin" />
|
||||
{:else}
|
||||
<KeyRound class="h-4 w-4" />
|
||||
{/if}
|
||||
Change Password
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{#if isOwner}
|
||||
@@ -911,6 +1419,62 @@
|
||||
Edit
|
||||
</button>
|
||||
</div>
|
||||
<div class="rounded bg-neutral-800/50 p-4">
|
||||
<div class="flex justify-between gap-1">
|
||||
<div class="flex flex-col">
|
||||
<span class="font-medium text-neutral-200">Password Reset Link</span>
|
||||
<span class="text-sm text-neutral-500">
|
||||
Send the user a reset link or copy one for manual delivery.
|
||||
</span>
|
||||
</div>
|
||||
<div class="flex items-center justify-end">
|
||||
<button
|
||||
onclick={generatePasswordResetLink}
|
||||
disabled={passwordResetLinkLoading}
|
||||
class="flex items-center gap-2 rounded bg-neutral-700 px-4 py-2 text-sm font-medium transition-colors hover:bg-neutral-600 disabled:opacity-50"
|
||||
>
|
||||
{#if passwordResetLinkLoading}
|
||||
<LoaderCircle class="h-4 w-4 animate-spin" />
|
||||
{:else}
|
||||
<KeyRound class="h-4 w-4" />
|
||||
{/if}
|
||||
Generate Reset Link
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{#if passwordResetLinkError}
|
||||
<div
|
||||
class="mt-3 rounded border border-red-900/50 bg-red-900/10 p-3 text-sm text-red-200"
|
||||
>
|
||||
{passwordResetLinkError}
|
||||
</div>
|
||||
{/if}
|
||||
{#if passwordResetLinkMessage}
|
||||
<div
|
||||
class="mt-3 rounded border border-green-900/50 bg-green-900/10 p-3 text-sm text-green-200"
|
||||
>
|
||||
{passwordResetLinkMessage}
|
||||
</div>
|
||||
{/if}
|
||||
|
||||
{#if passwordResetLinkValue}
|
||||
<div class="mt-3 flex items-center gap-2 rounded bg-neutral-900/70 p-2">
|
||||
<code class="flex-1 font-mono text-sm break-all text-green-300"
|
||||
>{passwordResetLinkValue}</code
|
||||
>
|
||||
<button
|
||||
class="p-1 text-neutral-400 hover:text-white"
|
||||
onclick={() => {
|
||||
navigator.clipboard.writeText(passwordResetLinkValue);
|
||||
}}
|
||||
use:tooltip={'Copy reset link'}
|
||||
>
|
||||
<Copy class="h-4 w-4" />
|
||||
</button>
|
||||
</div>
|
||||
{/if}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{#if !isOwner && data.user?.roles && !hasRole(data.user.roles, roles.Admin)}
|
||||
|
||||
Reference in New Issue
Block a user