Skip to content

useEmailAuth

Handle the entire email/password lifecycle: sign in, sign up, link email addresses, reset passwords, and verify one-time codes.

Request

type Request = {
  hook: 'useEmailAuth',
  params: [options?: UseEmailHookOptions]
}
 
type UseEmailHookOptions = {
  emailVerificationRedirectTo?: string
  logoutOnError?: boolean
  recoverWalletAutomatically?: boolean
  onSuccess?: (data: EmailAuthResult | EmailVerificationResult) => void
  onError?: (error: OpenfortError) => void
  onSettled?: (data: EmailAuthResult | EmailVerificationResult | undefined | null, error: OpenfortError | null) => void
  throwOnError?: boolean
}
 
type OpenfortError = {
  message: string
  type: OpenfortErrorType
  data: { [key: string]: any }
  name: string
}
 
enum OpenfortErrorType {
  AUTHENTICATION_ERROR = "AUTHENTICATION_ERROR",
  WALLET_ERROR = "WALLET_ERROR",
  CONFIGURATION_ERROR = "CONFIGURATION_ERROR",
  VALIDATION_ERROR = "VALIDATION_ERROR",
}

Response

type Response = UseEmailAuthReturn
 
type UseEmailAuthReturn = PasswordStatusFlags & {
  signInEmail(options: SignInEmailOptions): Promise<EmailAuthResult>
  signUpEmail(options: SignUpEmailOptions): Promise<EmailAuthResult>
  verifyEmail(options: VerifyEmailOptions): Promise<EmailVerificationResult>
  linkEmail(options: LinkEmailOptions): Promise<EmailAuthResult>
  requestResetPassword(options: RequestResetPasswordOptions): Promise<EmailAuthResult>
  resetPassword(options: ResetPasswordOptions): Promise<EmailAuthResult>
  reset(): void
  requiresEmailVerification: boolean
  isAwaitingInput: boolean
}
 
type PasswordStatusFlags = {
  isLoading: boolean
  isError: boolean
  isSuccess: boolean
  error?: OpenfortError | null
}
 
type EmailAuthResult = {
  error?: OpenfortError
  user?: AuthPlayerResponse
  wallet?: UserWallet
  requiresEmailVerification?: boolean
}
 
type EmailVerificationResult = {
  email?: string
  error?: OpenfortError
}
 
type SignInEmailOptions = {
  email: string
  password: string
  emailVerificationRedirectTo?: string
  onSuccess?: (data: EmailAuthResult) => void
  onError?: (error: OpenfortError) => void
  onSettled?: (data: EmailAuthResult | undefined | null, error: OpenfortError | null) => void
  throwOnError?: boolean
  logoutOnError?: boolean
  recoverWalletAutomatically?: boolean
}
 
type SignUpEmailOptions = {
  email: string
  password: string
  name?: string
  emailVerificationRedirectTo?: string
  onSuccess?: (data: EmailAuthResult) => void
  onError?: (error: OpenfortError) => void
  onSettled?: (data: EmailAuthResult | undefined | null, error: OpenfortError | null) => void
  throwOnError?: boolean
  logoutOnError?: boolean
  recoverWalletAutomatically?: boolean
}
 
type VerifyEmailOptions = {
  email: string
  state: string
  onSuccess?: (data: EmailVerificationResult) => void
  onError?: (error: OpenfortError) => void
  onSettled?: (data: EmailVerificationResult | undefined | null, error: OpenfortError | null) => void
  throwOnError?: boolean
}
 
type LinkEmailOptions = {
  email: string
  password: string
  emailVerificationRedirectTo?: string
  onSuccess?: (data: EmailAuthResult) => void
  onError?: (error: OpenfortError) => void
  onSettled?: (data: EmailAuthResult | undefined | null, error: OpenfortError | null) => void
  throwOnError?: boolean
}
 
type RequestResetPasswordOptions = {
  email: string
  emailVerificationRedirectTo?: string
  onSuccess?: (data: EmailAuthResult) => void
  onError?: (error: OpenfortError) => void
  onSettled?: (data: EmailAuthResult | undefined | null, error: OpenfortError | null) => void
  throwOnError?: boolean
}
 
type ResetPasswordOptions = {
  email: string
  password: string
  state: string
  onSuccess?: (data: EmailAuthResult) => void
  onError?: (error: OpenfortError) => void
  onSettled?: (data: EmailAuthResult | undefined | null, error: OpenfortError | null) => void
  throwOnError?: boolean
}
 
type UserWallet = {
  address: `0x${string}`
  connectorType?: string
  walletClientType?: string
  id: string
  isAvailable: boolean
  isActive?: boolean
  isConnecting?: boolean
  recoveryMethod?: RecoveryMethod
  accountId?: string
  accountType?: AccountTypeEnum
  ownerAddress?: `0x${string}`
  implementationType?: string
  createdAt?: number
  salt?: string
}
 
type AuthPlayerResponse = {
  player?: {
    id: string
    object: 'player'
    name: string
    description?: string
    createdAt: number
    metadata?: { [key: string]: any }
    transactionIntents?: any[]
    accounts?: any[]
  }
  id: string
  object: 'player'
  createdAt: number
  linkedAccounts: LinkedAccount[]
}
 
type LinkedAccount = {
  provider: string
  email?: string
  address?: string
  externalUserId?: string
  verified?: boolean
  disabled: boolean
  walletClientType?: string
  connectorType?: string
  updatedAt?: number
  metadata?: { [key: string]: any }
}
 
enum RecoveryMethod {
  AUTOMATIC = "automatic",
  PASSWORD = "password",
  PASSKEY = "passkey",
}
 
enum AccountTypeEnum {
  SMART_ACCOUNT = "Smart Account",
  EOA = "Externally Owned Account",
}

Example

import { useEmailAuth } from "@openfort/react"
 
function EmailFlows() {
  const {
    signInEmail,
    signUpEmail,
    linkEmail,
    verifyEmail,
    requestResetPassword,
    resetPassword,
    reset,
    isLoading,
    isError,
    isSuccess,
    isAwaitingInput,
    requiresEmailVerification,
    error,
  } = useEmailAuth({
    emailVerificationRedirectTo: "https://app.example.com/auth/callback",
    throwOnError: true,
    onSuccess: () => {},
    onError: () => {},
    onSettled: () => {},
  })
 
  const handleSignIn = async (email: string, password: string) => {
    const result = await signInEmail({ email, password })
    if (result.requiresEmailVerification) {
      // prompt user to check inbox for the verification code
    }
  }
 
  return null
}