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
cd ios && pod install
β‘ 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]);
};
π 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
β 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>
);
}
π 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>
);
}
π 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
}
π§ Best Practice (Production)
β Hybrid Flow
try {
startListening(); // auto (SMS Retriever)
} catch (e) {
// fallback UI
}
π Security Notes
- No
READ_SMSpermission 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
Top comments (0)