Skip to content
LogoLogo

useEmailAuthOtp

Handles OTP-based email authentication for passwordless login flows. Users receive a one-time password via email to authenticate.

Usage

import { useEmailAuthOtp } from '@openfort/react-native';
 
function OtpLoginForm() {
  const { requestEmailOtp, signInEmailOtp, isLoading, isSuccess, error } = useEmailAuthOtp({
    onSuccess: ({ user }) => console.log('OTP auth successful:', user?.id),
    onError: (error) => console.error('OTP auth failed:', error?.message),
  });
 
  const [email, setEmail] = useState('');
  const [otp, setOtp] = useState('');
  const [otpSent, setOtpSent] = useState(false);
 
  const handleRequestOtp = async () => {
    await requestEmailOtp({ email });
    setOtpSent(true);
  };
 
  const handleSignIn = async () => {
    await signInEmailOtp({ email, otp });
  };
 
  if (!otpSent) {
    return <Button title="Send OTP" onPress={handleRequestOtp} disabled={isLoading} />;
  }
 
  return <Button title="Sign In" onPress={handleSignIn} disabled={isLoading} />;
}

Hook options

Pass these options when initializing the hook:

type UseEmailOtpHookOptions = {
  onSuccess?: (data: EmailOtpAuthResult) => void
  onError?: (error: OpenfortError) => void
  throwOnError?: boolean
}

Return type

The hook returns the following:

type UseEmailAuthOtpReturn = {
  requestEmailOtp(options: RequestEmailOtpOptions): Promise<EmailOtpAuthResult>
  signInEmailOtp(options: LoginWithEmailOtpOptions): Promise<EmailOtpAuthResult>
  reset(): void
  isLoading: boolean
  isError: boolean
  isSuccess: boolean
  error: OpenfortError | null
}
 
type EmailOtpAuthResult = {
  user?: User
  error?: OpenfortError
}

Parameters

requestEmailOtp

Sends an OTP to the specified email address.

type RequestEmailOtpOptions = {
  email: string
  onSuccess?: (data: EmailOtpAuthResult) => void
  onError?: (error: OpenfortError) => void
  throwOnError?: boolean
}

signInEmailOtp

Signs in the user using the email and OTP code.

type LoginWithEmailOtpOptions = {
  email: string
  otp: string
  onSuccess?: (data: EmailOtpAuthResult) => void
  onError?: (error: OpenfortError) => void
  throwOnError?: boolean
}