react-native-otp-auto-verify 0.1.8 → 0.1.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +452 -466
- package/lib/module/NativeOtpAutoVerify.ts +13 -11
- package/lib/module/index.js +8 -2
- package/lib/module/index.js.map +1 -1
- package/lib/typescript/src/NativeOtpAutoVerify.d.ts +2 -2
- package/lib/typescript/src/NativeOtpAutoVerify.d.ts.map +1 -1
- package/lib/typescript/src/index.d.ts +8 -2
- package/lib/typescript/src/index.d.ts.map +1 -1
- package/package.json +175 -183
- package/src/NativeOtpAutoVerify.ts +13 -11
- package/src/index.tsx +178 -172
package/README.md
CHANGED
|
@@ -1,466 +1,452 @@
|
|
|
1
|
-
# react-native-otp-auto-verify
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
No
|
|
8
|
-
|
|
9
|
-
With minimal dependencies and clean architecture, it integrates seamlessly into both React Native Old Architecture and the New Architecture (TurboModule) environments. The solution improves user experience by eliminating manual OTP entry on Android while maintaining strong server-side validation standards.
|
|
10
|
-
|
|
11
|
-
Works best for onboarding/login flows in **banking**, **fintech**, and authentication-heavy apps.
|
|
12
|
-
Supports both RN Old Architecture and React Native **New Architecture** (TurboModule).
|
|
13
|
-
**Android**: automatic OTP detection.
|
|
14
|
-
|
|
15
|
-
**iOS**: Uses iOS Security Code AutoFill (manual tap required).
|
|
16
|
-
|
|
17
|
-
**Connect:** [GitHub](https://github.com/kailas-rathod/react-native-otp-auto-verify) · [npm](https://www.npmjs.com/package/react-native-otp-auto-verify) · [Issues](https://github.com/kailas-rathod/react-native-otp-auto-verify/issues) · [License](https://github.com/kailas-rathod/react-native-otp-auto-verify/blob/main/LICENSE)
|
|
18
|
-
|
|
19
|
-
---
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
## Features
|
|
24
|
-
|
|
25
|
-
- ✅ **Automatic OTP detection**: receives OTP from matching SMS and exposes it as `otp` (hook) or `extractedOtp` (listener)
|
|
26
|
-
- ✅ **No SMS permissions**: no access to inbox, avoids sensitive permissions and reduces compliance friction
|
|
27
|
-
- ✅ **App hash security**: OTP SMS must end with your **11-character hash** (only your app can receive it)
|
|
28
|
-
- ✅ **Hook + Imperative API**: `useOtpVerification()` for screens, `activateOtpListener()` for custom flows
|
|
29
|
-
- ✅ **TypeScript**: typed options and return values
|
|
30
|
-
- ✅ **New Architecture ready**: TurboModule implementation; works with Old Architecture too
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
|
38
|
-
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
```
|
|
50
|
-
|
|
51
|
-
```
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
```sh
|
|
56
|
-
|
|
57
|
-
```
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
```
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
)
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
};
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
### `
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
-
|
|
372
|
-
-
|
|
373
|
-
|
|
374
|
-
##
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
```
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
##
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
## Contributing
|
|
454
|
-
|
|
455
|
-
See [`CONTRIBUTING.md`](CONTRIBUTING.md).
|
|
456
|
-
|
|
457
|
-
## Publishing
|
|
458
|
-
|
|
459
|
-
Maintainers: see [RELEASE_CHECKLIST.md](./RELEASE_CHECKLIST.md) before publishing a new version to npm.
|
|
460
|
-
|
|
461
|
-
## Keywords
|
|
462
|
-
react native otp auto verify, react native sms retriever api, automatic otp detection android, react native otp autofill, sms retriever react native, otp verification library react native, google play compliant otp library
|
|
463
|
-
|
|
464
|
-
## License
|
|
465
|
-
|
|
466
|
-
[MIT](./LICENSE) — see [LICENSE](./LICENSE) in the repo.
|
|
1
|
+
# react-native-otp-auto-verify
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/react-native-otp-auto-verify) [](https://www.npmjs.com/package/react-native-otp-auto-verify) [](https://github.com/kailas-rathod/react-native-otp-auto-verify/blob/main/LICENSE) [](https://www.typescriptlang.org/)
|
|
4
|
+
|
|
5
|
+
**react-native-otp-auto-verify** is a lightweight and secure React Native OTP auto-verification library for Android, built on the official Google SMS Retriever API. It enables automatic OTP detection without requiring READ_SMS or RECEIVE_SMS permissions, ensuring full Google Play Store compliance and enhanced user trust. Designed for modern authentication flows, this library is ideal for fintech apps, banking applications, e-commerce platforms, and secure login systems.
|
|
6
|
+
|
|
7
|
+
- **No permissions**: It requires zero SMS permissions from the user, making it compliant with strict Google Play Store policies.
|
|
8
|
+
|
|
9
|
+
With minimal dependencies and clean architecture, it integrates seamlessly into both React Native Old Architecture and the New Architecture (TurboModule) environments. The solution improves user experience by eliminating manual OTP entry on Android while maintaining strong server-side validation standards.
|
|
10
|
+
|
|
11
|
+
Works best for onboarding/login flows in **banking**, **fintech**, and authentication-heavy apps.
|
|
12
|
+
Supports both RN Old Architecture and React Native **New Architecture** (TurboModule).
|
|
13
|
+
**Android**: automatic OTP detection.
|
|
14
|
+
|
|
15
|
+
**iOS**: Uses iOS Security Code AutoFill (manual tap required).
|
|
16
|
+
|
|
17
|
+
**Connect:** [GitHub](https://github.com/kailas-rathod/react-native-otp-auto-verify) · [npm](https://www.npmjs.com/package/react-native-otp-auto-verify) · [Issues](https://github.com/kailas-rathod/react-native-otp-auto-verify/issues) · [License](https://github.com/kailas-rathod/react-native-otp-auto-verify/blob/main/LICENSE)
|
|
18
|
+
|
|
19
|
+
---
|
|
20
|
+
|
|
21
|
+
<img width="1536" height="1024" alt="otp" src="https://github.com/user-attachments/assets/e4908e99-e7d1-4a96-a6d2-b92c50090db0" />
|
|
22
|
+
|
|
23
|
+
## Features
|
|
24
|
+
|
|
25
|
+
- ✅ **Automatic OTP detection**: receives OTP from matching SMS and exposes it as `otp` (hook) or `extractedOtp` (listener)
|
|
26
|
+
- ✅ **No SMS permissions**: no access to inbox, avoids sensitive permissions and reduces compliance friction
|
|
27
|
+
- ✅ **App hash security**: OTP SMS must end with your **11-character hash** (only your app can receive it)
|
|
28
|
+
- ✅ **Hook + Imperative API**: `useOtpVerification()` for screens, `activateOtpListener()` for custom flows
|
|
29
|
+
- ✅ **TypeScript**: typed options and return values
|
|
30
|
+
- ✅ **New Architecture ready**: TurboModule implementation; works with Old Architecture too
|
|
31
|
+
|
|
32
|
+
---
|
|
33
|
+
|
|
34
|
+
## Platform Support
|
|
35
|
+
|
|
36
|
+
| Platform | Support | Notes |
|
|
37
|
+
| -------- | -------------- | ----------------------------------------------------- |
|
|
38
|
+
| Android | ✅ | Requires Google Play Services on device |
|
|
39
|
+
| iOS | ✅ Native Only | Uses iOS Security Code AutoFill (manual tap required) |
|
|
40
|
+
|
|
41
|
+
## Requirements
|
|
42
|
+
|
|
43
|
+
- React Native: **0.60+** (autolinking)
|
|
44
|
+
- Android: **minSdkVersion 24+**
|
|
45
|
+
- **TurboModule**: The library uses codegen/TurboModule. The native module must be built and linked. If it is not registered (e.g. build issue or unsupported setup), importing the library will throw at load time.
|
|
46
|
+
|
|
47
|
+
## Installation
|
|
48
|
+
|
|
49
|
+
```sh
|
|
50
|
+
npm install react-native-otp-auto-verify
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
# or
|
|
54
|
+
|
|
55
|
+
```sh
|
|
56
|
+
yarn add react-native-otp-auto-verify
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
# or
|
|
60
|
+
|
|
61
|
+
```sh
|
|
62
|
+
pnpm add react-native-otp-auto-verify
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## Usage
|
|
66
|
+
|
|
67
|
+
### 1) Get your app hash
|
|
68
|
+
|
|
69
|
+
SMS Retriever only delivers messages that include your **11-character app hash**.
|
|
70
|
+
|
|
71
|
+
```ts
|
|
72
|
+
import { getHash } from 'react-native-otp-auto-verify';
|
|
73
|
+
|
|
74
|
+
const hashes = await getHash();
|
|
75
|
+
const appHash = hashes[0]; // send this to your backend
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### 2) Format your OTP SMS
|
|
79
|
+
|
|
80
|
+
Your backend **must** include the app hash at the **end** of the SMS.
|
|
81
|
+
|
|
82
|
+
Requirements:
|
|
83
|
+
|
|
84
|
+
- Message must be **≤ 140 bytes**
|
|
85
|
+
- Must contain a **4–6 digit** OTP
|
|
86
|
+
- Must **end with** the app hash from `getHash()`
|
|
87
|
+
|
|
88
|
+
Recommended format:
|
|
89
|
+
|
|
90
|
+
```
|
|
91
|
+
Dear Kailas Rathod, 321500 is your OTP for mobile authentication. This OTP is valid for the next 15 minutes. Please DO NOT share it with anyone.
|
|
92
|
+
uW87Uq6teXc
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
Note: You do not need `<#>` at the start of the message.
|
|
96
|
+
|
|
97
|
+
### 3) Hook usage (recommended)
|
|
98
|
+
|
|
99
|
+
Start listening only while the OTP screen is visible (foreground).
|
|
100
|
+
|
|
101
|
+
```tsx
|
|
102
|
+
import React from 'react';
|
|
103
|
+
import { Text, View } from 'react-native';
|
|
104
|
+
import { useOtpVerification } from 'react-native-otp-auto-verify';
|
|
105
|
+
|
|
106
|
+
export function OtpScreen() {
|
|
107
|
+
const { hashCode, otp, timeoutError, error, startListening, stopListening } =
|
|
108
|
+
useOtpVerification({ numberOfDigits: 6 });
|
|
109
|
+
|
|
110
|
+
React.useEffect(() => {
|
|
111
|
+
void startListening();
|
|
112
|
+
return () => stopListening();
|
|
113
|
+
}, [startListening, stopListening]);
|
|
114
|
+
|
|
115
|
+
return (
|
|
116
|
+
<View>
|
|
117
|
+
{!!hashCode && <Text>Hash: {hashCode}</Text>}
|
|
118
|
+
{!!otp && <Text>OTP: {otp}</Text>}
|
|
119
|
+
{timeoutError && <Text>Timeout. Tap resend and try again.</Text>}
|
|
120
|
+
{!!error && <Text>Error: {error.message}</Text>}
|
|
121
|
+
</View>
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
### 4) Imperative usage
|
|
127
|
+
|
|
128
|
+
```ts
|
|
129
|
+
import {
|
|
130
|
+
activateOtpListener,
|
|
131
|
+
removeListener,
|
|
132
|
+
} from 'react-native-otp-auto-verify';
|
|
133
|
+
|
|
134
|
+
const sub = await activateOtpListener(
|
|
135
|
+
(sms, extractedOtp) => {
|
|
136
|
+
if (extractedOtp) {
|
|
137
|
+
console.log('OTP:', extractedOtp);
|
|
138
|
+
}
|
|
139
|
+
},
|
|
140
|
+
{ numberOfDigits: 6 }
|
|
141
|
+
);
|
|
142
|
+
|
|
143
|
+
// cleanup
|
|
144
|
+
sub.remove();
|
|
145
|
+
// or
|
|
146
|
+
removeListener();
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
🔹 Step 1 – Start OTP Listener
|
|
150
|
+
|
|
151
|
+
```ts
|
|
152
|
+
import { useOtpVerification } from 'react-native-otp-auto-verify';
|
|
153
|
+
|
|
154
|
+
const { startListening, stopListening, otp } = useOtpVerification();
|
|
155
|
+
|
|
156
|
+
useEffect(() => {
|
|
157
|
+
startListening();
|
|
158
|
+
|
|
159
|
+
return () => stopListening();
|
|
160
|
+
}, []);
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
## Create OTP Screen (Recommended Hook Method)
|
|
164
|
+
|
|
165
|
+
```ts
|
|
166
|
+
import React, { useEffect, useState } from 'react';
|
|
167
|
+
import { View, Text, TextInput, Button, Platform } from 'react-native';
|
|
168
|
+
import { useOtpVerification } from 'react-native-otp-auto-verify';
|
|
169
|
+
|
|
170
|
+
const OtpScreen = () => {
|
|
171
|
+
const [otpValue, setOtpValue] = useState('');
|
|
172
|
+
|
|
173
|
+
const { otp, hashCode, timeoutError, error, startListening, stopListening } =
|
|
174
|
+
useOtpVerification({ numberOfDigits: 6 });
|
|
175
|
+
|
|
176
|
+
// Start listener when screen opens
|
|
177
|
+
useEffect(() => {
|
|
178
|
+
if (Platform.OS === 'android') {
|
|
179
|
+
startListening();
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
return () => {
|
|
183
|
+
stopListening();
|
|
184
|
+
};
|
|
185
|
+
}, []);
|
|
186
|
+
|
|
187
|
+
// Auto verify when OTP received
|
|
188
|
+
useEffect(() => {
|
|
189
|
+
if (otp) {
|
|
190
|
+
setOtpValue(otp);
|
|
191
|
+
verifyOtp(otp);
|
|
192
|
+
}
|
|
193
|
+
}, [otp]);
|
|
194
|
+
|
|
195
|
+
const verifyOtp = async (code: string) => {
|
|
196
|
+
console.log('Verifying OTP:', code);
|
|
197
|
+
|
|
198
|
+
// Call your backend API here
|
|
199
|
+
// await api.post('/verify-otp', { otp: code })
|
|
200
|
+
};
|
|
201
|
+
|
|
202
|
+
return (
|
|
203
|
+
<View style={{ padding: 20 }}>
|
|
204
|
+
<Text>Enter OTP</Text>
|
|
205
|
+
|
|
206
|
+
<TextInput
|
|
207
|
+
value={otpValue}
|
|
208
|
+
onChangeText={setOtpValue}
|
|
209
|
+
keyboardType="number-pad"
|
|
210
|
+
maxLength={6}
|
|
211
|
+
textContentType="oneTimeCode"
|
|
212
|
+
autoComplete="sms-otp"
|
|
213
|
+
style={{
|
|
214
|
+
borderWidth: 1,
|
|
215
|
+
padding: 12,
|
|
216
|
+
marginVertical: 12,
|
|
217
|
+
}}
|
|
218
|
+
/>
|
|
219
|
+
|
|
220
|
+
<Button title="Verify" onPress={() => verifyOtp(otpValue)} />
|
|
221
|
+
|
|
222
|
+
{timeoutError && (
|
|
223
|
+
<Text style={{ color: 'red' }}>Timeout. Please resend OTP.</Text>
|
|
224
|
+
)}
|
|
225
|
+
|
|
226
|
+
{error && <Text style={{ color: 'red' }}>Error: {error.message}</Text>}
|
|
227
|
+
</View>
|
|
228
|
+
);
|
|
229
|
+
};
|
|
230
|
+
|
|
231
|
+
export default OtpScreen;
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
# Start OTP Listener in Screen
|
|
235
|
+
|
|
236
|
+
```ts
|
|
237
|
+
import React, { useEffect, useState } from 'react';
|
|
238
|
+
import { View, TextInput, Text } from 'react-native';
|
|
239
|
+
import { useOtpVerification } from 'react-native-otp-auto-verify';
|
|
240
|
+
|
|
241
|
+
export default function OtpScreen() {
|
|
242
|
+
const [otpValue, setOtpValue] = useState('');
|
|
243
|
+
|
|
244
|
+
const { otp, startListening, stopListening } = useOtpVerification({
|
|
245
|
+
numberOfDigits: 6,
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
useEffect(() => {
|
|
249
|
+
startListening(); // Start listening
|
|
250
|
+
|
|
251
|
+
return () => {
|
|
252
|
+
stopListening(); // Cleanup
|
|
253
|
+
};
|
|
254
|
+
}, []);
|
|
255
|
+
|
|
256
|
+
useEffect(() => {
|
|
257
|
+
if (otp) {
|
|
258
|
+
setOtpValue(otp); // OTP automatically retrieved here
|
|
259
|
+
console.log('Retrieved OTP:', otp);
|
|
260
|
+
}
|
|
261
|
+
}, [otp]);
|
|
262
|
+
|
|
263
|
+
return (
|
|
264
|
+
<View>
|
|
265
|
+
<TextInput
|
|
266
|
+
value={otpValue}
|
|
267
|
+
onChangeText={setOtpValue}
|
|
268
|
+
keyboardType="number-pad"
|
|
269
|
+
maxLength={6}
|
|
270
|
+
/>
|
|
271
|
+
</View>
|
|
272
|
+
);
|
|
273
|
+
}
|
|
274
|
+
```
|
|
275
|
+
|
|
276
|
+
# iOS OTP AutoFill (Native)
|
|
277
|
+
|
|
278
|
+
iOS does **not** allow third-party libraries to read SMS messages.
|
|
279
|
+
|
|
280
|
+
Automatic SMS reading is restricted by Apple for privacy and security reasons.
|
|
281
|
+
Instead, iOS provides a native feature called **Security Code AutoFill**, which suggests the OTP above the keyboard when properly configured.
|
|
282
|
+
|
|
283
|
+
This library does **not** auto-read OTP on iOS.
|
|
284
|
+
|
|
285
|
+
---
|
|
286
|
+
|
|
287
|
+
## How iOS OTP AutoFill Works
|
|
288
|
+
|
|
289
|
+
1. User receives an SMS containing an OTP.
|
|
290
|
+
2. iOS detects the code.
|
|
291
|
+
3. The OTP appears above the keyboard.
|
|
292
|
+
4. User taps the suggestion.
|
|
293
|
+
5. The code fills automatically into the input field.
|
|
294
|
+
|
|
295
|
+
No SMS permissions required.
|
|
296
|
+
|
|
297
|
+
---
|
|
298
|
+
|
|
299
|
+
## React Native Setup
|
|
300
|
+
|
|
301
|
+
Use the following configuration in your OTP input field:
|
|
302
|
+
|
|
303
|
+
```tsx
|
|
304
|
+
<TextInput
|
|
305
|
+
style={styles.input}
|
|
306
|
+
keyboardType="number-pad"
|
|
307
|
+
textContentType="oneTimeCode"
|
|
308
|
+
autoComplete="sms-otp"
|
|
309
|
+
importantForAutofill="yes"
|
|
310
|
+
maxLength={6}
|
|
311
|
+
/>
|
|
312
|
+
```
|
|
313
|
+
|
|
314
|
+
## react-native-otp-auto-verify Architecture Flow
|
|
315
|
+
|
|
316
|
+
<img width="1536" height="1024" alt="react-native-otp-auto-verify Architecture Flow" src="https://github.com/user-attachments/assets/11582523-81cb-4904-9de0-56af05b3a3b4" />
|
|
317
|
+
|
|
318
|
+
## API Reference
|
|
319
|
+
|
|
320
|
+
### `useOtpVerification(options?)`
|
|
321
|
+
|
|
322
|
+
Use this on your OTP screen. It manages:
|
|
323
|
+
|
|
324
|
+
- getting the app hash (`hashCode`)
|
|
325
|
+
- starting/stopping the SMS Retriever listener
|
|
326
|
+
- extracting OTP and exposing it as `otp`
|
|
327
|
+
|
|
328
|
+
| Property | Type | Default | Description |
|
|
329
|
+
| ---------------- | --------------------- | ------- | ---------------------------------------- |
|
|
330
|
+
| `numberOfDigits` | `4 \| 5 \| 6` | `6` | OTP length to extract |
|
|
331
|
+
| `hashCode` | `string` | `''` | App hash (send to backend) |
|
|
332
|
+
| `otp` | `string \| null` | `null` | Extracted OTP |
|
|
333
|
+
| `sms` | `string \| null` | `null` | Full SMS text |
|
|
334
|
+
| `timeoutError` | `boolean` | `false` | Timeout occurred |
|
|
335
|
+
| `error` | `Error \| null` | `null` | Set when getHash or startListening fails |
|
|
336
|
+
| `startListening` | `() => Promise<void>` | — | Start listening |
|
|
337
|
+
| `stopListening` | `() => void` | — | Stop listening |
|
|
338
|
+
|
|
339
|
+
### `getHash(): Promise<string[]>`
|
|
340
|
+
|
|
341
|
+
Android only. On iOS returns `[]`.
|
|
342
|
+
|
|
343
|
+
### `activateOtpListener(handler, options?): Promise<{ remove(): void }>`
|
|
344
|
+
|
|
345
|
+
Android only. Throws on iOS.
|
|
346
|
+
|
|
347
|
+
### `removeListener(): void`
|
|
348
|
+
|
|
349
|
+
Android only. Removes native listeners.
|
|
350
|
+
|
|
351
|
+
### `extractOtp(sms: string, numberOfDigits?: 4 | 5 | 6): string | null`
|
|
352
|
+
|
|
353
|
+
Pure helper to extract the OTP from an SMS string.
|
|
354
|
+
|
|
355
|
+
## Timeout behavior
|
|
356
|
+
|
|
357
|
+
SMS Retriever waits up to **5 minutes**. When it times out:
|
|
358
|
+
|
|
359
|
+
- `timeoutError` becomes `true`
|
|
360
|
+
- call `startListening()` again to retry
|
|
361
|
+
|
|
362
|
+
## React Native New Architecture
|
|
363
|
+
|
|
364
|
+
This library is built with **TurboModules** and fully supports React Native's **New Architecture**.
|
|
365
|
+
|
|
366
|
+
### What is the New Architecture?
|
|
367
|
+
|
|
368
|
+
The New Architecture (also known as Fabric + TurboModules) is React Native's new rendering system and native module architecture that provides:
|
|
369
|
+
|
|
370
|
+
- Better performance and type safety
|
|
371
|
+
- Synchronous native module calls
|
|
372
|
+
- Improved interoperability with native code
|
|
373
|
+
|
|
374
|
+
## ✨ Feature Comparison
|
|
375
|
+
|
|
376
|
+
| Feature | react-native-otp-auto-verify | Other packages |
|
|
377
|
+
| ----------------------- | ---------------------------- | ------------------ |
|
|
378
|
+
| SMS Retriever API | ✅ Yes | ✅ Yes |
|
|
379
|
+
| Requires SMS Permission | ❌ No | ❌ No |
|
|
380
|
+
| TurboModule Support | ✅ Yes | ❌ Usually No |
|
|
381
|
+
| TypeScript Support | ✅ Full | ⚠️ Partial |
|
|
382
|
+
| Hook API | ✅ `useOtpVerification` | ❌ Not Available |
|
|
383
|
+
| App Hash Utility | ✅ Built-in | ⚠️ Basic |
|
|
384
|
+
| Architecture Ready | ✅ Old + New | ⚠️ Mostly Old Only |
|
|
385
|
+
| Maintenance | ✅ Actively Maintained | ⚠️ Varies |
|
|
386
|
+
|
|
387
|
+
### Enabling New Architecture
|
|
388
|
+
|
|
389
|
+
The library works automatically with both **Old Architecture** and **New Architecture**. No code changes needed.
|
|
390
|
+
|
|
391
|
+
**For Android:**
|
|
392
|
+
|
|
393
|
+
Enable New Architecture in `android/gradle.properties`:
|
|
394
|
+
|
|
395
|
+
```properties
|
|
396
|
+
newArchEnabled=true
|
|
397
|
+
```
|
|
398
|
+
|
|
399
|
+
**For iOS:**
|
|
400
|
+
|
|
401
|
+
Enable New Architecture in `ios/Podfile`:
|
|
402
|
+
|
|
403
|
+
```ruby
|
|
404
|
+
use_react_native!(
|
|
405
|
+
:fabric_enabled => true,
|
|
406
|
+
# ... other options
|
|
407
|
+
)
|
|
408
|
+
```
|
|
409
|
+
|
|
410
|
+
Or set the environment variable:
|
|
411
|
+
|
|
412
|
+
```sh
|
|
413
|
+
RCT_NEW_ARCH_ENABLED=1 pod install
|
|
414
|
+
```
|
|
415
|
+
|
|
416
|
+
### Compatibility
|
|
417
|
+
|
|
418
|
+
- ✅ Works with **Old Architecture** (React Native < 0.68)
|
|
419
|
+
- ✅ Works with **New Architecture** (React Native 0.68+)
|
|
420
|
+
- ✅ Automatically detects and uses the correct architecture
|
|
421
|
+
- ✅ No breaking changes when migrating
|
|
422
|
+
|
|
423
|
+
## Troubleshooting
|
|
424
|
+
|
|
425
|
+
- OTP not detected
|
|
426
|
+
- Ensure the SMS **ends with** the app hash
|
|
427
|
+
- Keep the SMS **≤ 140 bytes**
|
|
428
|
+
- Match `numberOfDigits` to your OTP length
|
|
429
|
+
- Keep the app in **foreground**
|
|
430
|
+
- Listener fails to start
|
|
431
|
+
- Ensure Google Play services are available on the device
|
|
432
|
+
- Avoid multiple active listeners at once
|
|
433
|
+
|
|
434
|
+
## Example app
|
|
435
|
+
|
|
436
|
+
See [`./example`](./example).
|
|
437
|
+
|
|
438
|
+
## Contributing
|
|
439
|
+
|
|
440
|
+
See [`CONTRIBUTING.md`](CONTRIBUTING.md).
|
|
441
|
+
|
|
442
|
+
## Publishing
|
|
443
|
+
|
|
444
|
+
Maintainers: see [RELEASE_CHECKLIST.md](./RELEASE_CHECKLIST.md) before publishing a new version to npm.
|
|
445
|
+
|
|
446
|
+
## Keywords
|
|
447
|
+
|
|
448
|
+
react native otp auto verify, react native sms retriever api, automatic otp detection android, react native otp autofill, sms retriever react native, otp verification library react native, google play compliant otp library
|
|
449
|
+
|
|
450
|
+
## License
|
|
451
|
+
|
|
452
|
+
[MIT](./LICENSE) — see [LICENSE](./LICENSE) in the repo.
|