Sign-in card
An email and password sign-in card with generic Continue with buttons (passkey and SSO, lucide icons, no brand logos), a show or hide password toggle, remember me, a forgot password link and a link to sign up. Fields validate on blur and on submit with messages tied to each input, the first invalid field takes focus, and the button shows a loading state. onSubmit can return a message to show as a form error; the demo rejects the password wrong-password.
Source
"use client";
import { useId, useRef, useState, type FormEvent, type ReactNode } from "react";
import { Building2, CircleAlert, CircleCheck, Eye, EyeOff, Fingerprint, LoaderCircle, type LucideIcon } from "lucide-react";
import { cn } from "@/lib/utils";
export type SignInValues = { email: string; password: string; remember: boolean };
export type SignInProvider = { id: string; label: string; icon: LucideIcon };
export type SignInCardProps = {
title?: string;
logo?: ReactNode;
providers?: SignInProvider[];
forgotHref?: string;
signUpHref?: string;
/** Resolve to sign in; resolve with a message (or throw) to show it as a form error. */
onSubmit?: (values: SignInValues) => Promise<string | void> | string | void;
onProvider?: (id: string) => Promise<void> | void;
className?: string;
};
const DEFAULT_PROVIDERS: SignInProvider[] = [
{ id: "passkey", label: "Continue with a passkey", icon: Fingerprint },
{ id: "sso", label: "Continue with SSO", icon: Building2 },
];
const EMAIL = /^[^\s@]+@[^\s@]+\.[^\s@]{2,}$/;
const focusRing = "outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background";
const wait = (ms: number) => new Promise<void>((resolve) => setTimeout(resolve, ms));
function check(values: SignInValues) {
return {
email: !values.email.trim() ? "Enter your email address." : !EMAIL.test(values.email.trim()) ? "Enter an email like name@company.com." : "",
password: !values.password ? "Enter your password." : values.password.length < 8 ? "Passwords are at least 8 characters." : "",
};
}
// Demo behaviour: any valid email works, except the password "wrong-password".
async function demoSubmit(values: SignInValues) {
await wait(1100);
if (values.password === "wrong-password") return "That email and password do not match. Try again or reset your password.";
}
export function SignInCard({
title = "Sign in to Paperlark",
logo,
providers = DEFAULT_PROVIDERS,
forgotHref = "#forgot",
signUpHref = "#sign-up",
onSubmit = demoSubmit,
onProvider = () => wait(1000),
className,
}: SignInCardProps) {
const id = useId();
const emailRef = useRef<HTMLInputElement>(null);
const passwordRef = useRef<HTMLInputElement>(null);
const [values, setValues] = useState<SignInValues>({ email: "", password: "", remember: true });
const [touched, setTouched] = useState({ email: false, password: false });
const [showPassword, setShowPassword] = useState(false);
const [pending, setPending] = useState<string | null>(null);
const [formError, setFormError] = useState("");
const [signedIn, setSignedIn] = useState(false);
const errors = check(values);
const visible = { email: touched.email ? errors.email : "", password: touched.password ? errors.password : "" };
const busy = pending !== null;
const submit = async (event: FormEvent<HTMLFormElement>) => {
event.preventDefault();
setTouched({ email: true, password: true });
setFormError("");
if (errors.email) return emailRef.current?.focus();
if (errors.password) return passwordRef.current?.focus();
setPending("form");
try {
const message = await onSubmit({ ...values, email: values.email.trim() });
if (message) setFormError(message);
else setSignedIn(true);
} catch (cause) {
setFormError(cause instanceof Error ? cause.message : "Something went wrong. Try again.");
} finally {
setPending(null);
}
};
const provider = async (providerId: string) => {
setPending(providerId);
try {
await onProvider(providerId);
} finally {
setPending(null);
}
};
const field = (name: "email" | "password") =>
cn(
"h-11 w-full rounded-md border bg-background px-3 text-sm text-foreground placeholder:text-muted-foreground transition-colors",
visible[name] ? "border-destructive" : "border-input hover:border-foreground/30",
focusRing,
);
if (signedIn)
return (
<div className={cn("w-full max-w-sm rounded-xl bg-card p-8 text-center text-card-foreground ring-1 ring-border", className)} role="status">
<CircleCheck className="mx-auto size-10 text-emerald-600 dark:text-emerald-400" aria-hidden="true" />
<p className="mt-4 text-xl font-semibold tracking-tight">You're signed in</p>
<p className="mt-2 text-sm text-muted-foreground">Taking you to your workspace…</p>
<button type="button" onClick={() => setSignedIn(false)} className={cn("mt-6 rounded-sm text-sm font-medium underline underline-offset-4", focusRing)}>
Back to sign in
</button>
</div>
);
return (
<div className={cn("w-full max-w-sm rounded-xl bg-card p-6 text-card-foreground ring-1 ring-border sm:p-8", className)}>
{logo && <div className="mb-6">{logo}</div>}
<h1 className="text-2xl font-semibold tracking-tight">{title}</h1>
{providers.length > 0 && (
<>
<div className="mt-6 grid gap-2">
{providers.map(({ id: providerId, label, icon: Icon }) => (
<button
key={providerId}
type="button"
disabled={busy}
onClick={() => provider(providerId)}
className={cn(
"inline-flex h-11 items-center justify-center gap-2.5 rounded-md bg-background px-4 text-sm font-medium text-foreground ring-1 ring-border transition-colors hover:bg-accent disabled:opacity-60",
focusRing,
)}
>
{pending === providerId ? <LoaderCircle className="size-4 motion-safe:animate-spin" aria-hidden="true" /> : <Icon className="size-4" aria-hidden="true" />}
{label}
</button>
))}
</div>
<div className="my-6 flex items-center gap-3 text-xs text-muted-foreground" role="separator" aria-label="or">
<span className="h-px flex-1 bg-border" />
or
<span className="h-px flex-1 bg-border" />
</div>
</>
)}
<form noValidate onSubmit={submit} className={cn("space-y-5", providers.length === 0 && "mt-6")}>
{formError && (
<p role="alert" className="flex gap-2 rounded-md bg-destructive/10 p-3 text-sm text-destructive">
<CircleAlert className="mt-0.5 size-4 shrink-0" aria-hidden="true" />
{formError}
</p>
)}
<div>
<label htmlFor={`${id}-email`} className="text-sm font-medium">Email</label>
<input
ref={emailRef}
id={`${id}-email`}
type="email"
inputMode="email"
autoComplete="email"
placeholder="name@company.com"
value={values.email}
onChange={(event) => setValues((current) => ({ ...current, email: event.target.value }))}
onBlur={() => setTouched((current) => ({ ...current, email: true }))}
aria-invalid={visible.email ? true : undefined}
aria-describedby={visible.email ? `${id}-email-error` : undefined}
className={cn("mt-2", field("email"))}
/>
{visible.email && <p id={`${id}-email-error`} className="mt-2 text-sm text-destructive">{visible.email}</p>}
</div>
<div>
<div className="flex items-baseline justify-between gap-3">
<label htmlFor={`${id}-password`} className="text-sm font-medium">Password</label>
<a href={forgotHref} className={cn("rounded-sm text-sm text-muted-foreground underline-offset-4 hover:text-foreground hover:underline", focusRing)}>
Forgot password?
</a>
</div>
<div className="relative mt-2">
<input
ref={passwordRef}
id={`${id}-password`}
type={showPassword ? "text" : "password"}
autoComplete="current-password"
value={values.password}
onChange={(event) => setValues((current) => ({ ...current, password: event.target.value }))}
onBlur={() => setTouched((current) => ({ ...current, password: true }))}
aria-invalid={visible.password ? true : undefined}
aria-describedby={visible.password ? `${id}-password-error` : undefined}
className={cn("pr-11", field("password"))}
/>
<button
type="button"
onClick={() => setShowPassword((value) => !value)}
aria-label={showPassword ? "Hide password" : "Show password"}
aria-pressed={showPassword}
aria-controls={`${id}-password`}
className={cn("absolute inset-y-0 right-0 grid w-11 place-items-center rounded-r-md text-muted-foreground hover:text-foreground", focusRing)}
>
{showPassword ? <EyeOff className="size-4" aria-hidden="true" /> : <Eye className="size-4" aria-hidden="true" />}
</button>
</div>
{visible.password && <p id={`${id}-password-error`} className="mt-2 text-sm text-destructive">{visible.password}</p>}
</div>
<label className="flex cursor-pointer items-center gap-2.5 text-sm">
<input
type="checkbox"
checked={values.remember}
onChange={(event) => setValues((current) => ({ ...current, remember: event.target.checked }))}
className={cn("size-4 cursor-pointer rounded accent-primary", focusRing)}
/>
Keep me signed in for 30 days
</label>
<button
type="submit"
disabled={busy}
className={cn(
"inline-flex h-11 w-full items-center justify-center gap-2 rounded-md bg-primary px-4 text-sm font-semibold text-primary-foreground transition-opacity hover:opacity-90 disabled:opacity-70",
focusRing,
)}
>
{pending === "form" && <LoaderCircle className="size-4 motion-safe:animate-spin" aria-hidden="true" />}
{pending === "form" ? "Signing in…" : "Sign in"}
</button>
</form>
<p className="mt-6 text-center text-sm text-muted-foreground">
New to Paperlark?{" "}
<a href={signUpHref} className={cn("rounded-sm font-medium text-foreground underline underline-offset-4 hover:no-underline", focusRing)}>
Create an account
</a>
</p>
</div>
);
}
