faizalshap / react-native-otp-verify

React native sms verification without additional permissions
MIT License
239 stars 92 forks source link

When using useOtpVerify hook, retrieved code is always 4 digits length #79

Closed mkurczewski closed 1 year ago

mkurczewski commented 1 year ago

There's a bug in useOtpVerify hook - numberOfDigits parameter is in fact not used at all. Look at line 62 for regex /(\d{4})/g - 4 is hardcoded while there should be numberOfDigits variable used, so instead of

https://github.com/faizalshap/react-native-otp-verify/blob/aff1f30f843144e0910d2fa467028953d36ca7be/src/index.tsx#L62

it should be

const regex = new RegExp("(\\d{" + numberOfDigits + "})", "g")
const otpDigits = regex.exec(response)
faizalshap commented 1 year ago

Hi @mkurczewski that’s correct Would you like to create a PR and contribute to the repo?

khokhas commented 1 year ago

this worked for me

useEffect(() => {
    startOtpListener(message => {
        if (message) {
            const otp = /(\d{6})/g.exec(message)![1];
            setOTP(otp.split(''));
        }
    });
    return () => removeListener();
}, []);
faizalshap commented 1 year ago

Fixed with PR