MagicLinkCallback

The page a sign-in link opens: trades the link’s token for a session, then continues where the user was going.

Status: beta. Category: Auth.

import { MagicLinkCallback } from '@glyphui/auth/ui'

Exports: MagicLinkCallback.

Notes

  • By default it reads ?token= from the URL; pass token (or param) when your link differs.
  • With React Router: onSuccess={useRedirectAfterSignIn("/app")} continues to ?next= or your default.
  • Accounts with two-step sign-in continue to the code step on the same page.
  • Needs an adapter with capabilities.magicLink. The REST adapter posts the token to /auth/magic-link/verify.
  • A link works once: React’s development double effects don’t spend it twice.

MagicLinkCallback props

PropTypeDefaultDescription
classNamestring
descriptionReactNode
footerReactNode
headingLevelHeadingLevelHeading level of the title. Default 1, for a page whose main content is the form.
onSuccess((session: AuthSession) => void)Called once the user is signed in, e.g. to continue to ?next=.
paramstringtokenQuery parameter that carries the token. Default token.
signInHrefstringAdds "Back to sign in" when the link can't be used.
titleReactNode
tokenstring | nullThe token from the sign-in link. Default: the param query parameter of the current URL.

Example: Open a sign-in link

Send a link to [email protected], then open it: the callback signs in and reports success. Opening it twice shows the expired state.

import { AuthProvider, useAuth } from '@glyphui/auth'
import { memoryAdapter } from '@glyphui/auth/memory'
import { MagicLinkCallback } from '@glyphui/auth/ui'
import { Button } from '@glyphui/ui'
import { useState } from 'react'

const adapter = memoryAdapter({
  latency: 400,
  users: [{ email: '[email protected]', password: 'correct horse', name: 'Ada Lovelace' }],
  onCode: () => {},
})

function Demo() {
  const { signIn, signOut, user } = useAuth()
  const [token, setToken] = useState<string | null>(null)
  const [opened, setOpened] = useState(0)

  if (opened > 0 && token)
    return (
      <div className="grid w-full max-w-sm gap-3">
        <MagicLinkCallback key={opened} token={token} headingLevel={3} />
        <div className="flex flex-wrap justify-center gap-2">
          {user && (
            <Button variant="outline" size="sm" onClick={() => setOpened((n) => n + 1)}>
              Open the same link again
            </Button>
          )}
          <Button
            variant="ghost"
            size="sm"
            onClick={async () => {
              if (user) await signOut()
              setToken(null)
              setOpened(0)
            }}
          >
            Start over
          </Button>
        </div>
      </div>
    )

  return (
    <div className="grid justify-items-center gap-3">
      <Button
        onClick={async () => {
          await signIn({ email: '[email protected]', magicLink: true })
          setToken(adapter.lastCode('[email protected]', 'magic-link') ?? null)
        }}
      >
        Email me a sign-in link
      </Button>
      {token && (
        <Button variant="outline" onClick={() => setOpened(1)}>
          Open the link from the email
        </Button>
      )}
    </div>
  )
}

export default function Example() {
  return (
    <AuthProvider adapter={adapter} syncTabs={false}>
      <Demo />
    </AuthProvider>
  )
}

Live page: https://glyphui.xyz/components/magic-link-callback