aiellie

Search

Find a component, template or page

GitHub
Design

Login Form

One form to sign in or sign up: Google and GitHub buttons, or an email and Continue, which sends a verification code. Shows it working while it waits and an error when it fails. Comes as a form and as a dialog.

fig. 01 · open it full screen from the corner

Installation

npx shadcn@latest add "@aiellie/login-form"

shadcn needs the @aiellie registry in components.json first. npx aiellie add login-form adds it on its first run, then installs the same way.

Login form is one form for signing in and signing up: Google or GitHub, or an email and Continue, which sends a verification code. There is no password and no separate sign-up, so a new address simply makes an account. LoginForm is the form on its own, for a page of its own; LoginDialog puts it in a dialog, which is what the User menu opens when no one is signed in. It is only the interface: sending the code and checking it are yours.

Getting started

  1. Open it from a Sign in button

    LoginDialog brings no trigger, so open it from wherever Sign in lives. onSubmit gets the email, and the dialog closes once it resolves.

    components/sign-in.tsx
    "use client"
    
    import * as React from "react"
    
    import { LoginDialog } from "@/components/ui/login-form"
    import { Button } from "@/components/ui/button"
    
    export function SignIn() {
      const [open, setOpen] = React.useState(false)
    
      return (
        <>
          <Button variant="outline" onClick={() => setOpen(true)}>
            Sign in
          </Button>
          <LoginDialog
            open={open}
            onOpenChange={setOpen}
            onSubmit={({ email }) => sendCode(email)}
            onProvider={(id) => signInWith(id)}
          />
        </>
      )
    }
  2. Say why it failed

    Throw from onSubmit, or return a promise that rejects, and the error's message shows above the button. The field keeps what was typed and the dialog stays open.

    <LoginDialog
      onSubmit={async ({ email }) => {
        const response = await fetch("/api/send-code", {
          method: "POST",
          body: JSON.stringify({ email }),
        })
        if (!response.ok) throw new Error("Couldn't send a code to that address.")
      }}
      {...props}
    />

Anatomy

<div data-slot="login-dialog"> {/* DialogContent, 384px wide at most */}
  <div data-slot="dialog-header">
    <h2 data-slot="dialog-title">{title}</h2> {/* Sign in or sign up */}
  </div>
  <form data-slot="login-form">
    <div data-slot="login-form-providers">
      <button /> {/* Continue with Google, one per provider */}
    </div>
    <div data-slot="login-form-divider" /> {/* or */}
    <label /> <input type="email" />
    <p /> {/* We'll email you a verification code to sign in. */}
    <p data-slot="login-form-error" /> {/* only after a failed attempt */}
    <button type="submit" /> {/* Continue */}
  </form>
</div>

Continue is the view's one primary button; the providers are outline buttons above it. The line under the field says what Continue does, and the field points to it with aria-describedby. While onSubmit runs, every control is disabled and the button reads Sending code with a spinner.

Examples

When the code can't be sent

With providers={[]} it is the email alone. This one always fails, to show the error.

We'll email you a verification code to sign in.

Other providers

providers replaces Google and GitHub. Each has an id, which onProvider gets, a name and an icon: a component that draws an SVG, like the marks in icons.

import { brandIcons } from "@/components/aiellie/icons"

<LoginForm
  providers={[{ id: "google", name: "Google", icon: brandIcons.google }]}
  onProvider={(id) => signInWith(id)}
/>

API reference

LoginForm

PropTypeDefaultDescription
onSubmit(values: { email: string }) => void | Promise<void>Called with the email to send a code to. A rejection's message shows above the button.
providersLoginProvider[]Google, GitHubSign-in buttons above the email field. Pass [] for none.
onProvider(id: string) => voidRuns when a provider's button is pressed.
emailDescriptionReactNodeWe'll email you a verification code to sign in.Under the email field: what happens after Continue.

LoginDialog

PropTypeDefaultDescription
openbooleanrequiredWhether the dialog is open.
onOpenChange(open: boolean) => voidrequiredCalled when it closes, including after onSubmit resolves.
titleReactNode"Sign in or sign up"The dialog's title.
descriptionReactNodeUnder the title.
…Every LoginForm prop.