react-native-sms-consent-api
Version:
A lightweight React Native module for Android's SMS User Consent API. Easily retrieve verification codes from SMS without full SMS permissions.
165 lines (113 loc) âĸ 3.69 kB
Markdown
# đ˛ react-native-sms-consent-api
A secure and lightweight **React Native module** for reading **one-time passwords (OTP)** using Android's **SMS User Consent API** â **without SMS permission**.
Perfect for apps needing secure OTP verification (e.g., login, signup, password reset).

## ⨠Features
- â
No `RECEIVE_SMS` permission needed
- ⥠Automatically handles SMS consent dialog
- đ¤ Emits success/error events
- đŖ Hook-based API (`useSmsConsent`) or manual listener
- đŦ TypeScript definitions included
- âī¸ Works with Android 5.0+ (API 21+)
## đĻ Installation
```bash
npm install @react-native-community/sms-user-consent
# or
yarn add @react-native-community/sms-user-consent
```
## đ ī¸ Android Setup
### 1. Autolinking
No manual linking required if using React Native 0.60+.
### 2. Permissions
## đą Android Permissions Setup
To use the **SMS User Consent API**, your app must declare the following permissions in `AndroidManifest.xml`:
### đ§ Required Permissions
```xml
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
```
## đ§ Usage
### â
1. Using `useSmsConsent` Hook
```tsx
import React from 'react';
import { Text, View } from 'react-native';
import { useSmsConsent } from '@react-native-community/sms-user-consent';
export default function OTPComponent() {
const otp = useSmsConsent(); // autoStart defaults to true
return (
<View>
<Text>OTP: {otp}</Text>
</View>
);
}
```
### đšī¸ 2. Manual Start/Stop with autoStart = false
```tsx
import React, { useEffect, useState } from 'react';
import {
startSmsConsentWatcher,
stopSmsConsentWatcher,
SmsConsentEmitter,
Events,
useSmsConsent,
} from '@react-native-community/sms-user-consent';
export default function ManualOtpComponent() {
const otp = useSmsConsent(false); // â Don't auto-start
const [code, setCode] = useState<string | null>(null);
useEffect(() => {
const received = SmsConsentEmitter.addListener(
Events.SMS_CONSENT_RECEIVED,
(event) => {
console.log('â
Message received', event.message);
setCode(event.message);
stopSmsConsentWatcher();
received.remove();
}
);
startSmsConsentWatcher();
return () => {
stopSmsConsentWatcher();
received.remove();
};
}, []);
return <Text>OTP Received: {code}</Text>;
}
```
## đ API Reference
### `useSmsConsent(autoStart?: boolean): string | null`
- **Returns**: the retrieved message or OTP
- **autoStart**: set to `false` if you want to trigger `startSmsConsentWatcher()` manually.
## đ Event Emitter Constants
```ts
Events.SMS_CONSENT_RECEIVED // When message is successfully retrieved
Events.SMS_CONSENT_ERROR // When an error occurs (e.g., USER_CANCELED)
```
## â
Supported Platforms
| Platform | Supported |
|----------|-----------|
| Android | â
Yes (API 21+) |
| iOS | â No support (Android only) |
## đ¤ Contributing
Pull requests, bug reports, and feature suggestions welcome! [Open an issue](https://github.com/antosmamanktr/react-native-sms-user-consent/issues)
## đ References
- [Android SMS User Consent API](https://developers.google.com/identity/sms-retriever/overview#user-consent-api)
- [React Native Docs](https://reactnative.dev/)
## đ§âđģ Author
**Made with â¤ī¸ by Antos Maman**
- GitHub: [@antosmamanktr](https://github.com/antosmamanktr)
- Email: [antosmamanktr@gmail.com](mailto\:antosmamanktr@gmail.com)
## đ License
MIT License