fix sms otp retriver
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
import { useState, useEffect } from 'react';
|
||||
import { useState, useEffect, useRef } from 'react';
|
||||
import { Platform } from 'react-native';
|
||||
import { WalletType } from 'rnwalletman';
|
||||
import { startOtpSmsListener } from '../utils/smsRetriever';
|
||||
|
||||
export interface OTPBindCallbacks {
|
||||
onRequestOTP: (walletType: WalletType, params: any) => Promise<any>;
|
||||
@@ -57,6 +59,8 @@ export function useOTPBind(
|
||||
const [otpData, setOtpData] = useState<any>(null);
|
||||
const [errorMessage, setErrorMessage] = useState('');
|
||||
const [resendCountdown, setResendCountdown] = useState(0);
|
||||
const [smsListenKey, setSmsListenKey] = useState(0);
|
||||
const smsStopRef = useRef<(() => void) | null>(null);
|
||||
|
||||
const { onRequestOTP, onVerifyOTP, onSuccess, onError, isDebug = false } = callbacks;
|
||||
const {
|
||||
@@ -73,6 +77,14 @@ export function useOTPBind(
|
||||
if (initialMobile) setMobile(initialMobile);
|
||||
}, [initialMobile]);
|
||||
|
||||
const log = (...args: any[]) => {
|
||||
if (isDebug) console.log(`[${walletType}]`, ...args);
|
||||
};
|
||||
|
||||
const error = (...args: any[]) => {
|
||||
if (isDebug) console.error(`[${walletType}]`, ...args);
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
if (resendCountdown <= 0) return;
|
||||
const timer = setInterval(() => {
|
||||
@@ -81,18 +93,43 @@ export function useOTPBind(
|
||||
return () => clearInterval(timer);
|
||||
}, [resendCountdown]);
|
||||
|
||||
const shouldListenSms =
|
||||
Platform.OS === 'android' &&
|
||||
(step === 'otp' || (passwordBeforeOtp && otpSent && formStarted));
|
||||
|
||||
useEffect(() => {
|
||||
if (!shouldListenSms) {
|
||||
smsStopRef.current?.();
|
||||
smsStopRef.current = null;
|
||||
return;
|
||||
}
|
||||
let cancelled = false;
|
||||
(async () => {
|
||||
smsStopRef.current?.();
|
||||
const stop = await startOtpSmsListener(otpLength, (code) => {
|
||||
if (cancelled) return;
|
||||
setOtp(code);
|
||||
log('OTP from SMS Retriever:', code);
|
||||
});
|
||||
if (cancelled) {
|
||||
stop();
|
||||
return;
|
||||
}
|
||||
smsStopRef.current = stop;
|
||||
})();
|
||||
return () => {
|
||||
cancelled = true;
|
||||
smsStopRef.current?.();
|
||||
smsStopRef.current = null;
|
||||
};
|
||||
}, [shouldListenSms, smsListenKey, otpLength]);
|
||||
|
||||
useEffect(() => () => smsStopRef.current?.(), []);
|
||||
|
||||
const startResendCooldown = () => setResendCountdown(resendCooldown);
|
||||
|
||||
const clearError = () => setErrorMessage('');
|
||||
|
||||
const log = (...args: any[]) => {
|
||||
if (isDebug) console.log(`[${walletType}]`, ...args);
|
||||
};
|
||||
|
||||
const error = (...args: any[]) => {
|
||||
if (isDebug) console.error(`[${walletType}]`, ...args);
|
||||
};
|
||||
|
||||
const requestOTP = async () => {
|
||||
const withPassword = passwordBeforeOtp && formStarted && passwordRequired;
|
||||
const isResend = otpSent && formStarted;
|
||||
@@ -136,6 +173,7 @@ export function useOTPBind(
|
||||
} else {
|
||||
setPasswordRequired(!!response.data?.needPassword || withPassword);
|
||||
setOtpSent(true);
|
||||
setSmsListenKey((k) => k + 1);
|
||||
startResendCooldown();
|
||||
if (!passwordBeforeOtp) {
|
||||
setStep('otp');
|
||||
@@ -216,6 +254,9 @@ export function useOTPBind(
|
||||
setOtpData(null);
|
||||
setErrorMessage('');
|
||||
setResendCountdown(0);
|
||||
setSmsListenKey(0);
|
||||
smsStopRef.current?.();
|
||||
smsStopRef.current = null;
|
||||
};
|
||||
|
||||
return [
|
||||
|
||||
Reference in New Issue
Block a user