-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
49 lines (38 loc) · 1.19 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import React, {useState, useEffect} from 'react';
import RNOtpVerify from 'react-native-otp-verify';
import {TextInput} from 'react-native'
// This works only android platform not supported for ios
//SMS format is must be like "<#> Confirmation code is 7555 6YE1NsOkHAl"
const AutoFill = () => {
const [value,setValue] = useState('')
useEffect(() => {
RNOtpVerify.getHash()
.then((e) => {
getCode(...e);
})
.catch((error) => {
getCode('');
console.log('error while getting otp hash key', error)
})
startListeningForOtp();
return () => RNOtpVerify.removeListener();
}, []);
const getCode = (key) => {
YourApi.get('/get-code',{
phone: '123456789',
phone_hash: key,
})
}
const startListeningForOtp = () =>
RNOtpVerify.getOtp()
.then((p) => RNOtpVerify.addListener(otpHandler))
.catch((p) => console.log(p))
const otpHandler = (message) => {
const otp = /(\d{4})/g.exec(message)[1];
setValue(otp);
RNOtpVerify.removeListener();
};
return (
<TextInput value={value} onChangeText={val=>setValue(val)} placeholder='Input code'/>
)
}