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)