ryuk-cli 0.1.1

The `ryuk` command: scaffold a Ryuk app (axum, SeaORM, Inertia/Svelte), generate controllers, models, jobs and mails, run migrations and the dev server.
<script>
  import { useForm, router } from "@inertiajs/svelte";
  import Field from "../../components/Field.svelte";
  import Icon from "../../components/Icon.svelte";
  import { registerPasskey } from "../../webauthn.js";
  let { grants = [], devices = [], passkeys = [], has_password = true, sessions = 0 } = $props();

  // The server sends Unix seconds (numbers); `new Date(number)` would read them as milliseconds (1970).
  const ms = (d) => (typeof d === "number" ? d * 1000 : Date.parse(d));
  const abs = (d) => (d ? new Date(ms(d)).toLocaleString() : "");
  const rtf = new Intl.RelativeTimeFormat(undefined, { numeric: "auto" });
  const UNITS = [["year", 31536e6], ["month", 2592e6], ["day", 864e5], ["hour", 36e5], ["minute", 6e4]];
  // Relative ("3 days ago", "in 2 hours"); the absolute time goes in a title attribute.
  const rel = (d) => {
    const diff = ms(d) - Date.now();
    const [u, size] = UNITS.find(([, s]) => Math.abs(diff) >= s) ?? ["minute", 6e4];
    return rtf.format(Math.round(diff / size), u);
  };
  const platform = (p) => ({ ios: "iOS", android: "Android" })[p] ?? p;

  const revoke = (kind, row, label) =>
    confirm(`Revoke ${label}? It is signed out immediately.`) &&
    router.post(`/settings/security/${kind}/${row.id}/revoke`, {}, { preserveScroll: true });

  const pw = useForm("post", "/settings/password", { current_password: "", password: "", password_confirmation: "" });
  const changePassword = (e) => {
    e.preventDefault();
    // Secrets never outlive the request, success or not.
    $pw.post("/settings/password", { preserveScroll: true, onFinish: () => $pw.reset() });
  };

  let confirmLogout = $state(false);
  let loggingOut = $state(false);
  const logoutAll = () =>
    router.post("/settings/security/logout-everywhere", {}, { onStart: () => (loggingOut = true), onFinish: () => { loggingOut = false; confirmLogout = false; } });

  let addingPasskey = $state(false);
  let passkeyError = $state("");
  const addPasskey = async () => {
    passkeyError = "";
    addingPasskey = true;
    try {
      const name = prompt("Name this passkey (e.g. \"MacBook Touch ID\"):", "");
      if (name === null) return;
      await registerPasskey(name || "Passkey");
      router.reload({ only: ["passkeys"] });
    } catch (e) {
      passkeyError = e.message || "Could not add this passkey.";
    } finally {
      addingPasskey = false;
    }
  };

  const del = useForm("post", "/settings/delete-account", { confirm: "", password: "" });
  const deleteAccount = (e) => {
    e.preventDefault();
    $del.post("/settings/delete-account", { onFinish: () => $del.reset("password") });
  };
</script>

<svelte:head><title>Security</title></svelte:head>
<div class="wrap page sec">
  <div class="page-head">
    <div>
      <span class="eyebrow">Settings</span>
      <h1>Security</h1>
      <p>Manage the apps and devices signed in to your account. {sessions} active {sessions === 1 ? "session" : "sessions"}.</p>
    </div>
  </div>

  <h2 class="list-h">Authorized apps</h2>
  <div class="card">
    {#if grants.length === 0}
      <div class="empty"><Icon name="shield" /><p>No apps have access to your account.</p></div>
    {:else}
      <ul class="rows">
        {#each grants as g (g.id)}
          <li>
            <div class="main">
              <strong>{g.client_name || g.client_id}</strong>
              <p>{g.scopes?.join(", ") || "no scopes"}</p>
              <p>
                Authorized <time datetime={g.created_at} title={abs(g.created_at)}>{rel(g.created_at)}</time>
                · {#if g.last_used_at}last used <time datetime={g.last_used_at} title={abs(g.last_used_at)}>{rel(g.last_used_at)}</time>{:else}never used{/if}
                {#if g.expires_at}· expires <time datetime={g.expires_at} title={abs(g.expires_at)}>{rel(g.expires_at)}</time>{/if}
              </p>
            </div>
            <div class="actions">
              <button type="button" class="btn btn-sm btn-danger" aria-label="Revoke {g.client_name || g.client_id}" onclick={() => revoke("grants", g, g.client_name || g.client_id)}>Revoke</button>
            </div>
          </li>
        {/each}
      </ul>
    {/if}
  </div>

  <h2 class="list-h">Push devices</h2>
  <div class="card">
    {#if devices.length === 0}
      <div class="empty"><Icon name="phone" /><p>No devices are registered for push notifications.</p></div>
    {:else}
      <ul class="rows">
        {#each devices as d (d.id)}
          {@const name = d.name || `${platform(d.platform)} device`}
          <li>
            <div class="main">
              <strong><Icon name="phone" /> {name}</strong>
              <p>{platform(d.platform)}{d.app_id ? ` · ${d.app_id}` : ""}</p>
              <p>
                Added <time datetime={d.created_at} title={abs(d.created_at)}>{rel(d.created_at)}</time>
                · {#if d.last_seen_at}last seen <time datetime={d.last_seen_at} title={abs(d.last_seen_at)}>{rel(d.last_seen_at)}</time>{:else}never seen{/if}
              </p>
            </div>
            <div class="actions">
              <button type="button" class="btn btn-sm btn-danger" aria-label="Revoke {name}" onclick={() => revoke("devices", d, name)}>Revoke</button>
            </div>
          </li>
        {/each}
      </ul>
    {/if}
  </div>

  <h2 class="list-h">Passkeys</h2>
  <div class="card">
    {#if passkeys.length === 0}
      <div class="empty"><Icon name="key" /><p>No passkeys yet. Add one to sign in without a password.</p></div>
    {:else}
      <ul class="rows">
        {#each passkeys as p (p.id)}
          <li>
            <div class="main">
              <strong><Icon name="key" /> {p.name}</strong>
              <p>
                Added <time datetime={p.created_at} title={abs(p.created_at)}>{rel(p.created_at)}</time>
                · {#if p.last_used_at}last used <time datetime={p.last_used_at} title={abs(p.last_used_at)}>{rel(p.last_used_at)}</time>{:else}never used{/if}
              </p>
            </div>
            <div class="actions">
              <button type="button" class="btn btn-sm btn-danger" aria-label="Remove {p.name}" onclick={() => revoke("passkeys", p, p.name)}>Remove</button>
            </div>
          </li>
        {/each}
      </ul>
    {/if}
    {#if passkeyError}<p class="meta" style="color: var(--accent)">{passkeyError}</p>{/if}
    <div class="form-actions top">
      <button type="button" class="btn" disabled={addingPasskey} onclick={addPasskey}>
        {#if addingPasskey}<span class="spin" aria-hidden="true"></span>Adding…{:else}Add a passkey{/if}
      </button>
    </div>
  </div>

  <h2 class="list-h">{has_password ? "Change password" : "Set a password"}</h2>
  <div class="card form-card">
    {#if !has_password}<p class="meta">You signed up with a social account. Set a password to also log in with your email.</p>{/if}
    <form onsubmit={changePassword}>
      {#if has_password}
        <Field id="current_password" label="Current password" type="password" autocomplete="current-password" bind:value={$pw.current_password} error={$pw.errors.current_password} />
      {/if}
      <Field id="new_password" label="New password" type="password" autocomplete="new-password" minlength="8" bind:value={$pw.password} error={$pw.errors.password} />
      <Field id="new_password_confirmation" label="Confirm new password" type="password" autocomplete="new-password" bind:value={$pw.password_confirmation} error={$pw.errors.password_confirmation} />
      <p class="meta">Changing your password signs out your other sessions and revokes authorized apps.</p>
      <div class="form-actions">
        <button type="submit" class="btn btn-primary" disabled={$pw.processing}>
          {#if $pw.processing}<span class="spin" aria-hidden="true"></span>Saving…{:else}Update password{/if}
        </button>
      </div>
    </form>
  </div>

  <h2 class="list-h">Log out everywhere</h2>
  <div class="card form-card">
    <p>Ends every web session (including this one) and revokes all app tokens.</p>
    <div class="form-actions top">
      {#if !confirmLogout}
        <button type="button" class="btn btn-danger" onclick={() => (confirmLogout = true)}>Log out everywhere</button>
      {:else}
        <button type="button" class="btn btn-primary" disabled={loggingOut} onclick={logoutAll}>
          {#if loggingOut}<span class="spin" aria-hidden="true"></span>Logging out…{:else}Yes, log out everywhere{/if}
        </button>
        <button type="button" class="btn" disabled={loggingOut} onclick={() => (confirmLogout = false)}>Cancel</button>
      {/if}
    </div>
  </div>

  <h2 class="list-h danger-h">Delete account</h2>
  <div class="card form-card danger-card">
    <p class="warn"><Icon name="alert" /> This permanently deletes your account, posts, API keys, devices and sessions. It cannot be undone.</p>
    <form onsubmit={deleteAccount}>
      <Field id="delete_confirm" label="Type DELETE to confirm" autocomplete="off" autocapitalize="characters" spellcheck="false" bind:value={$del.confirm} error={$del.errors.confirm} />
      {#if has_password}
        <Field id="delete_password" label="Password" type="password" autocomplete="current-password" bind:value={$del.password} error={$del.errors.password} />
      {/if}
      <div class="form-actions">
        <button type="submit" class="btn btn-danger" disabled={$del.processing || $del.confirm !== "DELETE"}>
          {#if $del.processing}<span class="spin" aria-hidden="true"></span>Deleting…{:else}Delete my account{/if}
        </button>
      </div>
    </form>
  </div>
</div>

<style>
  .sec :global(.rows strong svg) { width: 1rem; height: 1rem; vertical-align: -.15em; }
  .sec :global(.rows .main p) { display: block; }
  .top { margin-top: var(--s-3); }
  .danger-card { border-color: var(--accent); }
  .danger-h { color: var(--accent); }
  .warn { display: flex; gap: .5rem; align-items: flex-start; margin-bottom: var(--s-4); color: var(--accent); font-weight: 600; }
  .warn :global(svg) { flex: none; width: 1.25rem; height: 1.25rem; }
</style>