close

DEV Community

Kailas Rathod
Kailas Rathod

Posted on • Edited on

๐Ÿš€ Boost Login UX with Auto OTP Verification in React Native: react-native-otp-auto-verify (Complete Guide)

OTP (One-Time Password) flows are criticalโ€”but poorly implemented OTP UX leads to drop-offs, delays, and frustration.

This guide shows how to implement automatic OTP verification in React Native using:

  • SMS Retriever (auto)
  • SMS User Consent (fallback)
  • โœ… With real working code

๐Ÿ” Why Auto OTP Matters

Without auto verification:

  • Users switch apps
  • Copy/paste OTP
  • Make errors

With auto verification:

  • โšก Instant login
  • ๐ŸŽฏ Zero errors
  • ๐Ÿ“ˆ Better conversion

๐Ÿ“ฆ Install Library

npm install react-native-otp-auto-verify
Enter fullscreen mode Exit fullscreen mode
cd ios && pod install
Enter fullscreen mode Exit fullscreen mode

โšก Step 1: Get App Hash (IMPORTANT)

This hash must be added to your OTP SMS.

import { getHash } from 'react-native-otp-auto-verify';

const fetchHash = async () => {
  const hashes = await getHash();
  console.log('App Hash:', hashes[0]);
};
Enter fullscreen mode Exit fullscreen mode

๐Ÿ‘‰ Send this hash to your backend
๐Ÿ‘‰ Backend must append it to SMS (Medium)


๐Ÿ“ฉ SMS Format (Backend)

Dear User, 123456 is your OTP for login.

FA+9qCX9VSu
Enter fullscreen mode Exit fullscreen mode

โœ” Last line = app hash
โœ” Required for auto detection


โšก Step 2: Auto OTP Detection (Recommended Hook)

import React, { useEffect } from 'react';
import { View, Text } from 'react-native';
import { useOtpVerification } from 'react-native-otp-auto-verify';

export default function OtpScreen() {
  const {
    hashCode,
    otp,
    error,
    timeoutError,
    startListening,
    stopListening,
  } = useOtpVerification({ numberOfDigits: 6 });

  useEffect(() => {
    startListening();

    return () => {
      stopListening();
    };
  }, []);

  return (
    <View>
      <Text>Hash: {hashCode}</Text>

      {otp ? (
        <Text>โœ… OTP Received: {otp}</Text>
      ) : (
        <Text>Waiting for OTP...</Text>
      )}

      {timeoutError && <Text>โฑ Timeout, retry</Text>}
      {error && <Text>โŒ Error: {error.message}</Text>}
    </View>
  );
}
Enter fullscreen mode Exit fullscreen mode

๐Ÿ‘‰ This listens for SMS and auto-fills OTP
๐Ÿ‘‰ No SMS permission required (Medium)


โšก Step 3: Extract OTP into Input Field

Example with input:

import React, { useEffect, useState } from 'react';
import { View, TextInput } from 'react-native';
import { useOtpVerification } from 'react-native-otp-auto-verify';

export default function OtpInputScreen() {
  const [code, setCode] = useState('');

  const { otp, startListening, stopListening } =
    useOtpVerification({ numberOfDigits: 6 });

  useEffect(() => {
    startListening();
    return () => stopListening();
  }, []);

  useEffect(() => {
    if (otp) {
      setCode(otp); // auto fill
      console.log('Auto OTP:', otp);
    }
  }, [otp]);

  return (
    <View>
      <TextInput
        value={code}
        onChangeText={setCode}
        placeholder="Enter OTP"
        keyboardType="numeric"
      />
    </View>
  );
}
Enter fullscreen mode Exit fullscreen mode

๐Ÿ‘† Step 4: Fallback Strategy (Important)

Auto detection may fail if:

  • SMS format incorrect
  • Delay in SMS
  • No hash

๐Ÿ‘‰ Always add fallback:

if (!otp) {
  // show manual input + resend button
}
Enter fullscreen mode Exit fullscreen mode

๐Ÿง  Best Practice (Production)

โœ… Hybrid Flow

try {
  startListening(); // auto (SMS Retriever)
} catch (e) {
  // fallback UI
}
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”’ Security Notes

  • No READ_SMS permission required
  • Only messages with your app hash are read
  • OTP must always be verified on backend (Medium)

๐Ÿ Conclusion

To build a production-grade OTP system:

โœ” Use SMS Retriever โ†’ best UX
โœ” Add manual fallback โ†’ reliability
โœ” Format SMS correctly โ†’ critical


๐Ÿ“ฆ GitHub

๐Ÿ‘‰ https://github.com/kailas-rathod/react-native-otp-auto-verify


๐Ÿ”ฅ Final Takeaway

Auto OTP verification is not just a featureโ€”itโ€™s a UX upgrade.

Implement it correctly, and your login flow becomes:
๐Ÿ‘‰ Faster
๐Ÿ‘‰ Cleaner
๐Ÿ‘‰ Conversion-friendly


ReactNative #OTP #MobileDevelopment #AndroidDev #UX #Authentication

Top comments (0)