faizalshap / react-native-otp-verify

React native sms verification without additional permissions
MIT License
251 stars 95 forks source link

not able to detect otp by using react-native otp verify package. #51

Closed aakash-3990 closed 2 years ago

aakash-3990 commented 2 years ago
*To achieve the auto verification in react native I had been used the react-native-OTP-verify library But I not able to achieve it in my App.
Please help me with the same.

**My code is**

import RNOtpVerify from 'react-native-otp-verify';

componentDidMount() {
    if (Platform.OS === 'android') {
      this.getHash();
      this.startListeningForOtp();
    }
}

componentWillUnmount() {
    RNOtpVerify.removeListener();
}

getHash = () => {
    RNOtpVerify.getHash()
    .then(console.log)
    .catch(console.log);
}

startListeningForOtp = () => {
    RNOtpVerify.getOtp()
    .then(p => RNOtpVerify.addListener(this.otpHandler))
    .catch(p => console.log(p));
}

otpHandler = (message) => {
  if(message){
    const otp = /(\d{6})/g.exec(message)[1];
    this.setState({otp});
    RNOtpVerify.removeListener();
    Keyboard.dismiss();
  }
}

render() {
    const {otp, userName} = this.state;
    return (
<TextInput
                      // placeholder="Enter OTP"
                      style={styles.otpTextInput}
                      autoComplete='sms-otp' // for android
                      textContentType='oneTimeCode' //for ios
                      keyboardType="numeric"
                      autoFocus={true}
                      maxLength={6}
                      value={otp}
                      letterSpacing={12}
                      onChangeText={code => this.setState({otp: code})}
                    />
);

In this I get only the hash key but I don't know where I can use it Please guide me for the same.*

I am using this library to achieve the auto verification in react native.

enter code here
faizalshap commented 2 years ago

Hi @aakash-3990 you need to add that hash in the sms please read documentation from google that is mentioned in readme thanks