UNPKG

rn-sms-reader

Version:

This package reads Android text messages.

123 lines (118 loc) 4.31 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _reactNative = require("react-native"); /** * Error message displayed when the package 'rn-sms-reader' is not linked. * Instructions for linking are provided based on the platform. */ const ERROR_MESSAGE = `The package 'rn-sms-reader' doesn't seem to be linked. Make sure: \n\n` + _reactNative.Platform.select({ ios: "- You have run 'pod install'\n", default: '' }) + '- You rebuilt the app after installing the package\n' + '- You are not using Expo Go'; /** * Access the RnSmsReader module or throw an error if not linked. */ const RnSmsReader = _reactNative.NativeModules.RnSmsReader ? _reactNative.NativeModules.RnSmsReader : new Proxy({}, { get() { throw new Error(ERROR_MESSAGE); } }); /** * Define SMS permissions for checking and requesting. */ const receiveSmsPermission = _reactNative.PermissionsAndroid.PERMISSIONS.RECEIVE_SMS; const readSmsPermission = _reactNative.PermissionsAndroid.PERMISSIONS.READ_SMS; /** * Extracts OTP from SMS text using a regular expression. * @param text - SMS text from which OTP is to be extracted. * @param otpLength - Length of the OTP to be extracted. * @returns Extracted OTP or null if not found. */ const extractOtpFromText = (text, otpLength) => { const regex = new RegExp(`\\b\\d{${otpLength}}\\b`); const match = text.match(regex); return match ? match[0] : null; }; /** * RnSmsService provides functions to check, request SMS permissions, * and fetch SMS messages to extract OTP. */ const RnSmsService = { /** * Checks if SMS permissions are already granted. * @returns A Promise that resolves to true if both permissions are granted, false otherwise. */ checkingSmsPermission: async () => { const receiveSmsGranted = await _reactNative.PermissionsAndroid.check(receiveSmsPermission); const readSmsGranted = await _reactNative.PermissionsAndroid.check(readSmsPermission); return receiveSmsGranted && readSmsGranted; }, /** * Requests SMS permissions and returns a Promise indicating success. * @returns A Promise that resolves to true if permissions are granted, false otherwise. */ requestSmsPermission: async () => { try { const checkPermission = await RnSmsService.checkingSmsPermission(); if (checkPermission) { return true; } else { const granted = await _reactNative.PermissionsAndroid.requestMultiple([receiveSmsPermission, readSmsPermission]); if (granted[receiveSmsPermission] === _reactNative.PermissionsAndroid.RESULTS.GRANTED && granted[readSmsPermission] === _reactNative.PermissionsAndroid.RESULTS.GRANTED) { console.log('Read SMS permission granted::: ', granted); return true; } else { console.log('Read SMS permission denied::: ', granted); return false; } } } catch (error) { console.error('Error requesting SMS permissions::: ', error); return false; } }, /** * Fetches SMS messages and extracts OTP. * @param length - Length of the OTP to be extracted from the SMS. * @returns A Promise that resolves to an object containing the extracted OTP or undefined. */ fetchSms: async length => { return new Promise(async resolve => { let otp = undefined; const status = await RnSmsService.requestSmsPermission(); if (status) { try { RnSmsReader.initiateSmsReading(() => { const subscription = new _reactNative.NativeEventEmitter(RnSmsReader).addListener('on_sms_received', sms => { otp = extractOtpFromText(sms, length); subscription.remove(); console.log('OTP::: ', otp); resolve({ otp }); }); }, error => { console.log('Error while reading SMS::: ', error); resolve({ otp }); }); } catch (error) { console.log('Error while reading SMS::: ', error); resolve({ otp }); } } else { resolve({ otp }); } }); } }; var _default = exports.default = RnSmsService; //# sourceMappingURL=index.js.map