Skip to content
LogoLogo

usePhoneAuthOtp

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

Usage

import { usePhoneAuthOtp } from '@openfort/react-native';
 
function PhoneOtpLoginForm() {
  const { requestPhoneOtp, signInPhoneOtp, isLoading, isSuccess, error } = usePhoneAuthOtp({
    onSuccess: ({ user }) => console.log('Phone OTP auth successful:', user?.id),
    onError: (error) => console.error('Phone OTP auth failed:', error?.message),
  });
 
  const [phone, setPhone] = useState('');
  const [otp, setOtp] = useState('');
  const [otpSent, setOtpSent] = useState(false);
 
  const handleRequestOtp = async () => {
    await requestPhoneOtp({ phone });
    setOtpSent(true);
  };
 
  const handleSignIn = async () => {
    await signInPhoneOtp({ phone, 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 UsePhoneOtpHookOptions = {
  onSuccess?: (data: PhoneOtpAuthResult) => void
  onError?: (error: OpenfortError) => void
  throwOnError?: boolean
}

Return type

The hook returns the following:

type UsePhoneAuthOtpReturn = {
  requestPhoneOtp(options: RequestPhoneOtpOptions): Promise<PhoneOtpAuthResult>
  signInPhoneOtp(options: LoginWithPhoneOtpOptions): Promise<PhoneOtpAuthResult>
  reset(): void
  isLoading: boolean
  isError: boolean
  isSuccess: boolean
  error: OpenfortError | null
}
 
type PhoneOtpAuthResult = {
  user?: User
  error?: OpenfortError
}

Parameters

requestPhoneOtp

Sends an OTP via SMS to the specified phone number.

type RequestPhoneOtpOptions = {
  phone: string
  onSuccess?: (data: PhoneOtpAuthResult) => void
  onError?: (error: OpenfortError) => void
  throwOnError?: boolean
}

signInPhoneOtp

Signs in the user using the phone number and OTP code.

type LoginWithPhoneOtpOptions = {
  phone: string
  otp: string
  onSuccess?: (data: PhoneOtpAuthResult) => void
  onError?: (error: OpenfortError) => void
  throwOnError?: boolean
}