<script>
import { useForm, Link } from "@inertiajs/svelte";
import Logo from "../../components/Logo.svelte";
import Field from "../../components/Field.svelte";
import Social from "../../components/Social.svelte";
import { loginWithPasskey } from "../../webauthn.js";
let { social = [] } = $props();
const form = useForm("post", "/login", { email: "", password: "" });
const resend = useForm("post", "/email/resend", { email: "" });
const unverified = $derived($form.errors.unverified === "true");
let passkeyBusy = $state(false);
let passkeyError = $state("");
const signInWithPasskey = async () => {
passkeyError = "";
passkeyBusy = true;
try {
await loginWithPasskey();
} catch (e) {
passkeyError = e.message || "Passkey sign-in failed.";
passkeyBusy = false;
}
};
</script>
<svelte:head><title>Log in</title></svelte:head>
<div class="auth">
<div class="card auth-card">
<Logo class="brand-mark" />
<h1>Welcome back</h1>
<p class="lede">Log in to pick up where you left off.</p>
<Social providers={social} />
<form onsubmit={(e) => { e.preventDefault(); $form.post("/login"); }}>
<Field id="email" label="Email" type="email" autocomplete="email" bind:value={$form.email} error={$form.errors.email} />
<Field id="password" label="Password" type="password" autocomplete="current-password" bind:value={$form.password} error={$form.errors.password} />
<button type="submit" class="btn btn-primary btn-block" disabled={$form.processing}>
{#if $form.processing}<span class="spin" aria-hidden="true"></span>Logging in…{:else}Log in{/if}
</button>
</form>
{#if unverified}
<p class="auth-alt">
<button type="button" class="btn btn-sm" disabled={$resend.processing}
onclick={() => { $resend.email = $form.email; $resend.post("/email/resend"); }}>Resend verification email</button>
</p>
{/if}
<button type="button" class="btn btn-block" disabled={passkeyBusy} onclick={signInWithPasskey}>
{#if passkeyBusy}<span class="spin" aria-hidden="true"></span>Waiting for your passkey…{:else}Sign in with a passkey{/if}
</button>
{#if passkeyError}<p class="auth-alt" style="color: var(--accent)">{passkeyError}</p>{/if}
<p class="auth-alt"><Link href="/forgot-password">Forgot your password?</Link><br />New here? <Link href="/register">Create an account</Link></p>
</div>
</div>