Skip to Content
TelamonJS

SignInForm & ForgotPasswordForm

Credentials sign-in over next-auth and a forgot-password form, localized, validated and safe against open redirects and account probing

The sign-in and forgot-password forms every app used to copy from the CLI template. They render the form only: branding, the card and any extra links go around them in your page.

// src/app/(auth)/sign-in/page.tsx — a Server Component is fine
import { SignInForm } from '@telamonjs/ui'

export default function SignInPage() {
  return (
    <div className='mx-auto mt-24 max-w-md rounded-lg border bg-background p-8'>
      <SignInForm callbackUrl='/admin' forgotPasswordHref='/forgot-password' />
    </div>
  )
}
// src/app/(auth)/forgot-password/page.tsx
'use client'
import { ForgotPasswordForm } from '@telamonjs/ui'
import { authApi } from '@/lib/api/auth-client'

export default function ForgotPasswordPage() {
  return (
    <ForgotPasswordForm
      onSubmit={(email) => authApi.requestPasswordReset(email)}
      signInHref='/sign-in'
    />
  )
}

Both read their text from the library messages (telamonjs.auth.*, telamonjs.validation.*), so the page must sit under AppProviders (the generated root layout already does this).


SignInForm

Behavior

  • Submit calls signIn('credentials', { [identifierName]: identifier, password, redirect: false, callbackUrl }) from next-auth/react, then router.push(url) and router.refresh(). The button stays pending until the page changes.
  • Destination: a ?callbackUrl in the address bar wins when it points to the same origin (relative, or absolute on this host). Anything else (another host, //host, javascript:) is ignored, then callbackUrl, then '/'.
  • Validation (zod): both fields required; with identifierType='email' the format is checked too. Errors render under each input and are linked with aria-describedby.
  • Errors: a CredentialsSignin result shows the localized "invalid credentials"; any other error, or a thrown one, shows a generic failure. errorMessages overrides by code first, then by error, which covers custom CredentialsSignin subclasses thrown in authorize():
// src/auth.ts
class LockedError extends CredentialsSignin {
  code = 'locked'
}

// the page
<SignInForm errorMessages={{ locked: msg('auth.locked') }} />
  • Password toggle: an icon button that switches the field between hidden and visible, with a localized aria-label.

Username, CPF or another identifier

<SignInForm
  identifierName='login'
  identifierType='text'
  identifierLabel={msg('auth.username')}
  normalizeIdentifier={(value) => value.replace(/\D/g, '')}
/>

normalizeIdentifier runs on the trimmed value right before sign-in.

Custom navigation or authentication

onSuccess replaces the router navigation. Use it for a full reload or a home that depends on the role:

<SignInForm
  onSuccess={async (url) => {
    const session = await getSession()
    window.location.assign(url === '/' ? homeForRole(session?.user.role) : url)
  }}
/>

onSubmit replaces the signIn call. Resolve { error, code } to show an error (mapped as above), or anything else to continue to onSuccess / navigation.

Props

PropTypeDefault
identifierNamestring, the key sent to the credentials provider'email'
identifierLabelI18nTextlocalized "Email"
identifierType'email' | 'text' | 'tel''email'
identifierPlaceholderI18nText—
passwordPlaceholderI18nText—
autoFocusboolean, focuses the identifier on mountfalse
normalizeIdentifier(value: string) => string—
callbackUrlstring, used when the URL has no same-origin ?callbackUrl'/'
forgotPasswordHrefstring, renders the "Forgot your password?" linkno link
errorMessagesRecord<string, I18nText | ReactNode>, by error code, then error; a node can carry an action link—
onSubmit(values: SignInFormValues) => Promise<SignInFormResult>next-auth signIn
onSuccess(url: string) => void | Promise<void>router.push + router.refresh
classNamestring, on the <form>—
submitClassNamestring, on the submit button (merged with w-full)—
errorClassNamestring | (code) => string | undefined, merged over the destructive alert style; the function form restyles one error code—

SignInFormValues is { identifier: string; password: string }. SignInFormResult is { error?: string | null; code?: string | null } | void, which next-auth's SignInResponse fits.


ForgotPasswordForm

  • Asks for an email (required, validated), then calls onSubmit(email) with the trimmed value. Your app sends the email.
  • On success it replaces the form with a neutral message ("If that email is registered...") in a role="status" region. Resolve onSubmit whether or not the account exists.
  • A rejection shows a generic localized error and keeps the form. The rejection's message is never shown, so it cannot leak whether an account exists.
PropTypeDefault
onSubmit(email: string) => Promise<unknown>, the resolved value is ignored—
emailPlaceholderI18nText—
autoFocusbooleanfalse
submitClassNamestring, on the submit button—
errorClassNamestring, merged over the alert style—
signInHrefstring, renders "Back to sign in" in both statesno link
classNamestring—
  • AdminShell: the shell the user lands in after signing in

On this page