react-native-otp-textinput vs react-native-otp-verify
React Native OTP Implementation Strategies
react-native-otp-textinputreact-native-otp-verify

React Native OTP Implementation Strategies

react-native-otp-textinput and react-native-otp-verify serve distinct but complementary roles in React Native OTP implementations. react-native-otp-textinput is a customizable UI component for rendering and managing OTP input fields, handling focus transitions and styling across digits. react-native-otp-verify is a native module that leverages Android's SMS Retriever API to automatically read OTP messages containing a specific app hash, eliminating manual entry on Android devices. Neither package replaces the other—they address different layers of the OTP experience: user interface versus system-level message interception.

Npm Package Weekly Downloads Trend

3 Years

Github Stars Ranking

Stat Detail

Package
Downloads
Stars
Size
Issues
Publish
License
react-native-otp-textinput018231.3 kB269 months agoMIT
react-native-otp-verify0286402 kB332 years agoMIT

Building OTP Experiences in React Native: UI Input vs SMS Auto-Verification

When implementing one-time password (OTP) flows in React Native apps, developers face two distinct challenges: collecting user input and automatically reading OTPs from SMS messages. The packages react-native-otp-textinput and react-native-otp-verify address these problems in fundamentally different ways — one is a UI component, the other is a native bridge for SMS reading. Confusing them leads to architectural mismatches. Let’s clarify their roles and how they fit into real-world authentication flows.

📱 Core Purpose: UI Component vs Native Module

react-native-otp-textinput is a pure JavaScript UI component that renders a series of text inputs for OTP entry. It handles focus management, styling, and input masking but has no awareness of SMS or device capabilities.

// react-native-otp-textinput: Rendering OTP fields
import OTPTextInput from 'react-native-otp-textinput';

function OTPScreen() {
  const handleOTPSubmit = (code) => {
    // Validate code against your backend
    console.log('User entered:', code);
  };

  return (
    <OTPTextInput
      handleTextChange={handleOTPSubmit}
      inputCount={6}
      containerStyle={{ marginTop: 20 }}
      textInputStyle={{ borderWidth: 1, borderRadius: 8 }}
    />
  );
}

react-native-otp-verify is a native module that interacts with Android’s SMS Retriever API to automatically read OTP messages without requiring SMS permissions. It provides no UI — only methods to start/stop listening for messages containing a specific hash.

// react-native-otp-verify: Reading OTP from SMS
import { getOtp, addListener } from 'react-native-otp-verify';

async function setupAutoRead() {
  try {
    // Start listening for SMS with your app's hash
    await getOtp();
    
    // Handle received OTP
    addListener((otp) => {
      console.log('Auto-read OTP:', otp);
      // Pass to your validation logic
    });
  } catch (error) {
    console.warn('SMS auto-read failed:', error);
  }
}

⚠️ Critical Note: react-native-otp-verify only works on Android and requires your backend to include a 11-character hash in OTP messages. iOS support is not possible due to platform restrictions.

🔌 Integration Workflow: How They Fit Together

In a production app, you’ll often use both packages together:

  1. Use react-native-otp-verify to auto-fill the OTP when possible (Android only).
  2. Fall back to react-native-otp-textinput for manual entry (all platforms).

Here’s how they integrate:

import React, { useState, useEffect } from 'react';
import OTPTextInput from 'react-native-otp-textinput';
import { getOtp, removeListener } from 'react-native-otp-verify';

function AuthScreen() {
  const [otp, setOtp] = useState('');

  useEffect(() => {
    // Try auto-read on mount (Android only)
    const setupAutoRead = async () => {
      try {
        await getOtp();
        // Listener would update `otp` state when SMS arrives
      } catch (e) {
        // Silently fail – user will enter manually
      }
    };
    
    setupAutoRead();
    
    return () => removeListener(); // Cleanup
  }, []);

  const handleSubmit = (code) => {
    // Validate OTP (whether auto-filled or manual)
    console.log('Validating:', code || otp);
  };

  return (
    <OTPTextInput
      handleTextChange={setOtp}
      inputCount={6}
      // Pre-fill if auto-read succeeded
      textInputProps={{ value: otp }}
    />
  );
}

⚙️ Technical Constraints and Gotchas

Platform Support

  • react-native-otp-textinput: Works identically on iOS and Android since it’s pure JS.
  • react-native-otp-verify: Android-only (relies on SMS Retriever API). Fails silently on iOS.

Permissions and Setup

  • react-native-otp-textinput: Zero native setup. Just install and use.
  • react-native-otp-verify: Requires:
    • Android Gradle configuration
    • Backend to append app-specific hash to SMS
    • No SMS permissions needed (uses Google Play Services)

Error Handling

  • react-native-otp-textinput: Handles input errors via props like tintColor for invalid states.
  • react-native-otp-verify: Throws errors if:
    • Device lacks Google Play Services
    • SMS doesn’t contain the correct hash
    • Timeout occurs (listens for 5 minutes by default)

🛠️ When to Use Which (or Both)

Use ONLY react-native-otp-textinput if:

  • You need a simple, cross-platform OTP input
  • Your team can’t modify backend SMS templates to include hashes
  • You’re targeting iOS primarily

Use ONLY react-native-otp-verify if:

  • You’re building an Android-only kiosk app
  • You want zero UI (e.g., silent background verification)

Use BOTH if:

  • You want the best UX: auto-fill on Android + manual fallback everywhere
  • Your backend can include the required hash in SMS

🚫 Common Misconceptions

  • Myth: "react-native-otp-verify replaces OTP input fields."
    Reality: It only provides the OTP string. You still need a UI component (like react-native-otp-textinput) to display it or handle manual entry.

  • Myth: "Both packages work on iOS."
    Reality: Apple’s privacy restrictions prevent automatic SMS reading. react-native-otp-verify will throw an error on iOS.

  • Myth: "I don’t need backend changes for auto-read."
    Reality: Without the 11-character hash in your SMS, react-native-otp-verify won’t detect messages.

💡 Pro Tips for Production

  1. Always fall back to manual input: Auto-read fails in ~15% of cases (per Google’s docs). Never block users.
  2. Pre-fill, don’t auto-submit: Even if you auto-read the OTP, let users review it before submitting.
  3. Test hash generation: Use Google’s hash generator tool to validate your backend’s implementation.
  4. Handle timeouts: react-native-otp-verify stops listening after 5 minutes. Restart if needed.

📊 Summary: Key Differences

Aspectreact-native-otp-textinputreact-native-otp-verify
TypeUI ComponentNative Module
Platform SupportiOS + AndroidAndroid Only
Backend ChangesNoneRequires SMS hash
PermissionsNoneNone (uses Play Services)
Primary RoleCollect user inputAuto-read SMS OTP
UI Included?YesNo

💎 Final Takeaway

These packages solve complementary problems, not competing ones. Think of react-native-otp-textinput as your OTP keyboard and react-native-otp-verify as your OTP clipboard reader. In most real-world apps, you’ll wire them together: auto-fill the clipboard when possible, but always keep the keyboard ready for manual entry. Ignoring this distinction leads to either broken Android flows or unnecessary complexity on iOS.

How to Choose: react-native-otp-textinput vs react-native-otp-verify

  • react-native-otp-textinput:

    Choose react-native-otp-textinput when you need a cross-platform, customizable UI component for OTP entry that works consistently on both iOS and Android. It’s ideal for projects where you control the frontend but can’t modify backend SMS templates to include app-specific hashes, or when you prioritize a consistent manual input experience across all devices.

  • react-native-otp-verify:

    Choose react-native-otp-verify when building Android-focused applications that require seamless OTP auto-reading without SMS permissions, provided your backend can append the required 11-character hash to SMS messages. Avoid it for iOS-only projects or if you cannot implement the necessary backend changes for hash generation.

README for react-native-otp-textinput

REACT NATIVE OTP TEXT INPUT

React Native Component that can used for OTPs and Pins as secure pin input.

npm version npm downloads

Installation

npm i -S react-native-otp-textinput

Demo


How to Use

Check the Example react native app for usage.

Platform Support

Supports both Android and iOS.

Props

The following props are applicable for the component along with props supported by react native TextInput component

PropTypeOptionalDefaultDescription
defaultValuestringYes''Default Value that can be set based on OTP / Pin received from parent container.
handleTextChangefuncNon/acallback with concated string of all cells as argument.
handleCellTextChangefuncYesn/acallback for text change in individual cell with cell text and cell index as arguments
inputCountnumberYes4Number of Text Input Cells to be present.
tintColorstringYes#3CB371Color for Cell Border on being focused.
offTintColorstringYes#DCDCDCColor for Cell Border Border not focused.
inputCellLengthnumberYes1Number of character that can be entered inside a single cell.
containerStyleobjectYes{}style for overall container.
textInputStyleobjectYes{}style for text input.
testIDPrefixstringYes'otpinput'testID prefix, the result will be otp_input_0 until inputCount
autoFocusboolYesfalseInput should automatically get focus when the components loads
onBlurfuncYesn/aCallback that is called when the text input is blurred.
onFocusfuncYesn/aCallback that is called when the text input is focused.
disabledfuncYesfalseTo disable the input

Helper Functions

Clearing and Setting values to component

// using traditional ref
clearText = () => {
    this.otpInput.clear();
}

setText = () => {
    this.otpInput.setValue("1234");
}

render() {
    return (
        <View>
            <OTPTextInput ref={e => (this.otpInput = e)} >
            <Button title="clear" onClick={this.clearText}>
        </View>
    );
}
// hooks
import React, { useRef } from 'react';

const ParentComponent = () => {
    let otpInput = useRef(null);

    const clearText = () => {
        otpInput.current.clear();
    }

    const setText = () => {
        otpInput.current.setValue("1234");
    }

    return (
        <View>
            <OTPTextInput ref={e => (otpInput = e)} >
            <Button title="clear" onClick={clearText}>
        </View>
    );
}

If you like the project

If you think I have helped you, feel free to get me coffee. 😊

Buy Me A Coffee