Settings

The Settings page is one of the most feature-rich areas of SvelteForge Admin, combining multiple management sections into a single tabbed interface. Built entirely with Svelte 5 runes and SvelteKit form actions, it covers profile management, password changes, session auditing, application configuration, and appearance preferences — all with progressive enhancement and server-side validation.

Each section is a separate tab powered by $state for reactive tab switching. All mutations use SvelteKit form actions with use:enhance for non-blocking submissions, and success/error feedback is delivered via svelte-sonner toast notifications.

Profile Settings

The Profile tab allows users to update their display name and email address. The form action validates inputs server-side, then hands the change to Better Auth through auth.api.updateUser() and auth.api.changeEmail(), passing the request headers so Better Auth knows which session is calling.

How It Works

  1. Display name — Required, 1-100 characters
  2. Email — Validated for format, lowercased, and checked for uniqueness by Better Auth
// Profile update form action (simplified)
updateProfile: async ({ request, locals }) => {
  // ... validate name and email ...

  try {
    await auth.api.updateUser({ body: { name }, headers: request.headers });

    if (newEmail !== locals.user.email) {
      // Unverified accounts switch immediately; verified ones get a confirmation link.
      await auth.api.changeEmail({
        body: { newEmail, callbackURL: "/settings" },
        headers: request.headers,
      });
    }
  } catch (err) {
    return authFailure(err);
  }

  return { success: true, action: "profile", emailPending };
}

Accounts with an unverified email (every email/password account in a default install) change email immediately. Accounts with a verified email, such as OAuth sign-ups, receive a confirmation link through sendAuthEmail() and keep the old address until they approve it. In demo mode the shared demo account cannot change its profile; the guard lives in Better Auth's hooks.before, so it also covers the raw /api/auth endpoints.

Password Change

The Password tab requires the current password for verification before accepting a new one. This prevents unauthorized password changes even if a session is compromised on a shared device.

Validation Rules

  • Current password — Verified against the stored Argon2id hash
  • New password — Minimum 8 characters, maximum 255 characters
  • Confirm password — Must exactly match the new password
// Password change form action (simplified)
changePassword: async ({ request }) => {
  // ... validate lengths (PASSWORD_MIN / PASSWORD_MAX) and confirmation ...

  try {
    await auth.api.changePassword({
      body: { currentPassword, newPassword, revokeOtherSessions: false },
      headers: request.headers,
    });
  } catch (err) {
    if (isAPIError(err) && err.body?.code === "INVALID_PASSWORD") {
      return fail(400, { message: "Current password is incorrect" });
    }
    return authFailure(err);
  }

  return { success: true, action: "password" };
}

Better Auth verifies the current password and stores the new one in accounts.password using the same Argon2id parameters as registration (19 MB memory cost, 2 iterations), configured through the emailAndPassword.password hooks in auth.ts.

Session Management

The Sessions tab provides a complete audit trail of all active sessions for the current user. This is a security-critical feature that gives users visibility into where their account is active and the ability to revoke suspicious sessions.

Session Information

Each session row displays:

  • Device type — Desktop, mobile, or tablet (detected from user-agent)
  • Browser — Name and version (Chrome 120, Firefox 121, Safari 17, etc.)
  • Operating system — Windows, macOS, Linux, iOS, Android
  • IP address — The IP recorded when the session was created
  • Started — Relative timestamp of when the session was created
  • Current session badge — Highlights the session you are currently using

User-agent parsing is handled by the parseUserAgent() utility in src/lib/utils/user-agent.ts. It extracts browser name/version, OS name/version, and device type from the raw user-agent string using pattern matching — no external parsing library required.

Revoking Sessions

Two revocation options are available:

  1. Revoke individual session — Deletes a specific session from the database. The current session cannot be revoked here; use logout instead.
  2. Revoke all other sessions — Keeps the current session active but deletes every other session for the user. This is the "log me out everywhere else" action.
// Revoke a single session
revokeSession: async ({ request, locals }) => {
  // Scope the lookup to the current user so one user can't revoke another's session.
  const target = db
    .select({ token: sessions.token })
    .from(sessions)
    .where(and(eq(sessions.id, sessionId), eq(sessions.userId, locals.user.id)))
    .get();
  if (!target) return fail(404, { message: "Session not found" });

  await auth.api.revokeSession({ body: { token: target.token }, headers: request.headers });
  return { success: true, action: "session" };
}

// Revoke all other sessions
revokeAllOtherSessions: async ({ request }) => {
  await auth.api.revokeOtherSessions({ headers: request.headers });
  return { success: true, action: "sessions" };
}

The session lookup always scopes to the current user's ID, preventing users from revoking sessions belonging to other accounts. The bearer token column is never sent to the browser; the page only receives the session id.

Session Metadata

Better Auth records the user agent and IP address when a session is created. The Sessions tab shows that device information alongside how long ago each session started.

App Settings (Admin-Only)

The App Settings tab is only visible to users with the admin role. It provides a key-value configuration interface backed by the appSettings table in Drizzle ORM.

Available Settings

KeyPurposeDefault
siteNameApplication display name shown in the sidebar and browser titleSvelteForge Admin
timezoneServer timezone for date formatting and schedulingUTC
defaultRoleRole assigned to newly registered usersviewer
maintenanceModeWhen "true", blocks all non-admin users with a 503 errorfalse

Upsert Pattern

Settings are saved using a Drizzle ORM upsert (insert with onConflictDoUpdate). This means the form action works identically whether a setting exists or is being created for the first time:

// App settings form action
appSettings: async ({ request, locals }) => {
  if (locals.user.role !== "admin") {
    return fail(403, { message: "Admin access required" });
  }

  const formData = await request.formData();
  const settings = Object.fromEntries(formData.entries());

  for (const [key, value] of Object.entries(settings)) {
    await db.insert(appSettings)
      .values({ key, value: value.toString() })
      .onConflictDoUpdate({
        target: appSettings.key,
        set: { value: value.toString() },
      });
  }

  return { success: true, message: "Settings saved" };
}

Maintenance mode is particularly powerful — toggling it on immediately blocks all non-admin users from accessing any route in the (app) route group. The auth guard in src/routes/(app)/+layout.server.ts checks this setting on every request and returns a 503 error for non-admin users. Admins retain full access to disable maintenance mode when ready.

Appearance

The Appearance section controls the dark/light mode toggle, powered by mode-watcher with system preference detection.

How It Works

  • Three modes — Light, Dark, and System (follows OS preference)
  • Persistence — Mode preference is stored in localStorage and persists across sessions
  • No flash — mode-watcher applies the theme before the page renders, preventing the flash of wrong theme

Svelte 5 Integration

A critical Svelte 5 pattern to note: mode-watcher v1 exports a mode object that is a runes-based reactive object, not a Svelte store. You must use mode.current to read the current mode — using the legacy $mode store syntax will not work:

// CORRECT — Svelte 5 runes pattern
import { mode } from "mode-watcher";

// In your component
const isDark = $derived(mode.current === "dark");

// WRONG — This is Svelte 4 store syntax, does NOT work
// $mode === "dark"  // <-- Will not compile in Svelte 5

The toggle component renders different icons based on the current mode and uses mode.set() to switch between "light", "dark", and "system".

Svelte 5 Patterns in Settings

The Settings page demonstrates several key Svelte 5 patterns that are used throughout SvelteForge Admin:

Tabbed Interface with $state

<script lang="ts">
  let activeTab = $state("profile");

  const tabs = [
    { id: "profile", label: "Profile", icon: UserIcon },
    { id: "password", label: "Password", icon: LockIcon },
    { id: "sessions", label: "Sessions", icon: MonitorIcon },
    { id: "appearance", label: "Appearance", icon: PaletteIcon },
    { id: "app", label: "App Settings", icon: SettingsIcon },
  ];
</script>

{#each tabs as tab}
  <button
    class:active={activeTab === tab.id}
    onclick={() => (activeTab = tab.id)}
  >
    <tab.icon class="size-4" />
    {tab.label}
  </button>
{/each}

{#if activeTab === "profile"}
  <!-- Profile form -->
{:else if activeTab === "password"}
  <!-- Password form -->
{/if}

Form Data with $state

<script lang="ts">
  let { data } = $props();

  let name = $state(data.user.name);
  let email = $state(data.user.email);
  let avatarUrl = $state(data.user.avatarUrl ?? "");
</script>

<form method="POST" action="?/profile" use:enhance>
  <input name="name" bind:value={name} />
  <input name="email" type="email" bind:value={email} />
  <input name="avatarUrl" bind:value={avatarUrl} />
  <button type="submit">Save Changes</button>
</form>

Progressive Enhancement with use:enhance

All forms on the Settings page use SvelteKit's use:enhance directive. This means forms work without JavaScript (full page reload on submit) and upgrade to AJAX-style submissions when JavaScript is available. The enhance callback handles toast notifications:

<form
  method="POST"
  action="?/profile"
  use:enhance={() => {
    return async ({ result, update }) => {
      if (result.type === "success") {
        toast.success("Profile updated successfully");
      } else if (result.type === "failure") {
        toast.error(result.data?.message ?? "Something went wrong");
      }
      await update();
    };
  }}
>

Need More?

Go Premium with DashboardPack

SvelteForge Admin provides a complete settings foundation with Svelte 5 and SvelteKit. Need profile picture upload with drag-and-drop, two-factor authentication (TOTP/SMS), billing management with Stripe integration, and team administration with invite flows? Our premium templates at DashboardPack include all of these and more.

  • Profile picture upload — Drag-and-drop with crop, resize, and CDN storage
  • Two-factor authentication — TOTP setup with QR code and backup codes
  • Billing management — Stripe subscription management with invoices
  • Team administration — Invite members, assign roles, manage permissions
  • Notification preferences — Granular email and in-app notification controls