kailas-rathod/react-native-otp-auto-verify

GitHub: kailas-rathod/react-native-otp-auto-verify

一款基于 Google SMS Retriever API 的轻量级 React Native 库,无需短信权限即可实现 Android 平台的 OTP 自动检测与填充。

Stars: 2 | Forks: 2

# react-native-otp-auto-verify [![npm 版本](https://img.shields.io/npm/v/react-native-otp-auto-verify.svg?style=flat-square)](https://www.npmjs.com/package/react-native-otp-auto-verify) [![npm 下载量](https://img.shields.io/npm/dm/react-native-otp-auto-verify.svg?style=flat-square)](https://www.npmjs.com/package/react-native-otp-auto-verify) [![许可证](https://img.shields.io/npm/l/react-native-otp-auto-verify.svg?style=flat-square)](https://github.com/kailas-rathod/react-native-otp-auto-verify/blob/main/LICENSE) [![typescript](https://img.shields.io/badge/TypeScript-Ready-blue.svg?style=flat-square)](https://www.typescriptlang.org/) **react-native-otp-auto-verify** 是一个轻量级且安全的 Android React Native OTP 自动验证库,基于官方 Google SMS Retriever API 构建。它无需 READ_SMS 或 RECEIVE_SMS 权限即可实现自动 OTP 检测,确保完全符合 Google Play Store 政策并增强用户信任。该库专为现代身份验证流程而设计,是金融科技应用、银行应用程序、电子商务平台和安全登录系统的理想选择。 无权限:它不需要用户授予任何 SMS 权限,符合严格的 Google Play Store 政策。 凭借极少的依赖和简洁的架构,它可以无缝集成到 React Native 旧架构和新架构(TurboModule)环境中。该解决方案通过消除 Android 上的手动 OTP 输入来改善用户体验,同时保持强大的服务端验证标准。 最适用于**银行**、**金融科技**和身份验证密集型应用的引导/登录流程。 支持 RN 旧架构和 React Native **新架构**(TurboModule)。 **Android**:自动 OTP 检测。 **iOS**:使用 iOS Security Code AutoFill(需要手动点击)。 **联系:** [GitHub](https://github.com/kailas-rathod/react-native-otp-auto-verify) · [npm](https://www.npmjs.com/package/react-native-otp-auto-verify) · [问题](https://github.com/kailas-rathod/react-native-otp-auto-verify/issues) · [许可证](https://github.com/kailas-rathod/react-native-otp-auto-verify/blob/main/LICENSE) otp ## 功能 - ✅ **自动 OTP 检测**:从匹配的 SMS 中接收 OTP,并将其作为 `otp`(hook)或 `extractedOtp`(监听器)暴露出来 - ✅ **无 SMS 权限**:不访问收件箱,避免敏感权限并减少合规摩擦 - ✅ **App Hash 安全性**:OTP SMS 必须以您的 **11 位 hash** 结尾(只有您的应用才能接收) - ✅ **Hook + 命令式 API**:用于屏幕的 `useOtpVerification()`,用于自定义流程的 `activateOtpListener()` - ✅ **TypeScript**:类型化的选项和返回值 - ✅ **新架构就绪**:TurboModule 实现;同时也适用于旧架构 ## 平台支持 | 平台 | 支持 | 备注 | |----------|----------|-------| | Android | ✅ | 设备需要安装 Google Play Services | | iOS | ✅ 仅原生 | 使用 iOS Security Code AutoFill(需要手动点击) | ## 环境要求 - React Native:**0.60+**(自动链接) - Android:**minSdkVersion 24+** ## 安装 ``` npm install react-native-otp-auto-verify ``` # 或者 ``` yarn add react-native-otp-auto-verify ``` # 或者 ``` pnpm add react-native-otp-auto-verify ``` ## 用法 ### 1) 获取您的 App Hash SMS Retriever 仅投递包含您 **11 位 App Hash** 的消息。 ``` import { getHash } from 'react-native-otp-auto-verify'; const hashes = await getHash(); const appHash = hashes[0]; // send this to your backend ``` ### 2) 格式化您的 OTP SMS 您的后端**必须**在 SMS 的**末尾**包含 App Hash。 要求: - 消息必须 **≤ 140 字节** - 必须包含 **4–6 位** OTP - 必须**以** `getHash()` 获取的 App Hash **结尾** 推荐格式: ``` 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. uW87Uq6teXc ``` 注意:您不需要在消息开头添加 `<#>`。 ### 3) Hook 用法(推荐) 仅在 OTP 屏幕可见(前台)时开始监听。 ``` import React from 'react'; import { Text, View } from 'react-native'; import { useOtpVerification } from 'react-native-otp-auto-verify'; export function OtpScreen() { const { hashCode, otp, timeoutError, error, startListening, stopListening } = useOtpVerification({ numberOfDigits: 6 }); React.useEffect(() => { void startListening(); return () => stopListening(); }, [startListening, stopListening]); return ( {!!hashCode && Hash: {hashCode}} {!!otp && OTP: {otp}} {timeoutError && Timeout. Tap resend and try again.} {!!error && Error: {error.message}} ); } ``` ### 4) 命令式用法 ``` import { activateOtpListener, removeListener, } from 'react-native-otp-auto-verify'; const sub = await activateOtpListener( (sms, extractedOtp) => { if (extractedOtp) { console.log('OTP:', extractedOtp); } }, { numberOfDigits: 6 } ); // cleanup sub.remove(); // or removeListener(); ``` 🔹 步骤 1 – 启动 OTP 监听器 ``` import { useOtpVerification } from 'react-native-otp-auto-verify'; const { startOtpListener, stopListener, otp } = useOtpVerification(); useEffect(() => { startOtpListener(); return () => stopListener(); }, []); ``` ## 创建 OTP 屏幕(推荐的 Hook 方法) ``` import React, { useEffect, useState } from 'react'; import { View, Text, TextInput, Button, Platform, } from 'react-native'; import { useOtpVerification } from 'react-native-otp-auto-verify'; const OtpScreen = () => { const [otpValue, setOtpValue] = useState(''); const { otp, hashCode, timeoutError, error, startListening, stopListening, } = useOtpVerification({ numberOfDigits: 6 }); // Start listener when screen opens useEffect(() => { if (Platform.OS === 'android') { startListening(); } return () => { stopListening(); }; }, []); // Auto verify when OTP received useEffect(() => { if (otp) { setOtpValue(otp); verifyOtp(otp); } }, [otp]); const verifyOtp = async (code: string) => { console.log('Verifying OTP:', code); // Call your backend API here // await api.post('/verify-otp', { otp: code }) }; return ( Enter OTP
标签:Android, App安全, DSL, Fintech, Google Play合规, iOS Security Code AutoFill, npm包, OTP自动验证, React Native, SMS Retriever API, TurboModule, TypeScript, 前端组件, 安全插件, 无权限读取短信, 用户体验优化, 用户认证, 电商, 登录注册, 短信验证码, 移动开发, 自动化攻击, 自动填充, 轻量级库, 金融科技, 银行App