expo-otp-retriever
Version:
Expo module for retrieving OTP codes on Android using Google's SMS Retriever API
97 lines • 4.41 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.OtpRetrieverErrorCodes = exports.OtpRetrieverEvents = void 0;
const expo_modules_core_1 = require("expo-modules-core");
const ExpoOtpRetrieverModule_1 = __importDefault(require("./ExpoOtpRetrieverModule"));
/**
* Events emitted by the OTP Retriever module
*/
var OtpRetrieverEvents;
(function (OtpRetrieverEvents) {
OtpRetrieverEvents["OTP_RECEIVED"] = "otpReceived";
OtpRetrieverEvents["OTP_TIMEOUT"] = "otpTimeout";
OtpRetrieverEvents["OTP_ERROR"] = "otpError";
})(OtpRetrieverEvents || (exports.OtpRetrieverEvents = OtpRetrieverEvents = {}));
/**
* Error codes that might be returned by the module
*/
var OtpRetrieverErrorCodes;
(function (OtpRetrieverErrorCodes) {
OtpRetrieverErrorCodes["LISTENER_ERROR"] = "LISTENER_ERROR";
OtpRetrieverErrorCodes["HASH_GENERATION_ERROR"] = "HASH_GENERATION_ERROR";
OtpRetrieverErrorCodes["UNSUPPORTED_PLATFORM"] = "UNSUPPORTED_PLATFORM";
OtpRetrieverErrorCodes["PLAY_SERVICES_UNAVAILABLE"] = "PLAY_SERVICES_UNAVAILABLE";
})(OtpRetrieverErrorCodes || (exports.OtpRetrieverErrorCodes = OtpRetrieverErrorCodes = {}));
// Create an event emitter to handle OTP events
const emitter = new expo_modules_core_1.EventEmitter(ExpoOtpRetrieverModule_1.default);
/**
* Main class for OTP Retriever functionality
*/
class OtpRetriever {
/**
* Starts listening for OTP SMS messages
* @param timeoutSeconds Optional timeout in seconds (default: 60)
* @returns Promise that resolves when listening starts successfully
*/
startListener() {
return __awaiter(this, arguments, void 0, function* (timeoutSeconds = 60) {
if (!ExpoOtpRetrieverModule_1.default) {
throw this._createError(OtpRetrieverErrorCodes.UNSUPPORTED_PLATFORM, 'OTP Retriever is only supported on Android');
}
return yield ExpoOtpRetrieverModule_1.default.startOtpListener(timeoutSeconds);
});
}
/**
* Stops listening for OTP SMS messages
* @returns Promise that resolves when listening stops successfully
*/
stopListener() {
return __awaiter(this, void 0, void 0, function* () {
if (!ExpoOtpRetrieverModule_1.default) {
throw this._createError(OtpRetrieverErrorCodes.UNSUPPORTED_PLATFORM, 'OTP Retriever is only supported on Android');
}
return yield ExpoOtpRetrieverModule_1.default.stopOtpListener();
});
}
/**
* Generates an app signature hash needed for SMS OTP format
* @returns Promise that resolves with the app hash string
*/
getAppHash() {
return __awaiter(this, void 0, void 0, function* () {
if (!ExpoOtpRetrieverModule_1.default) {
throw this._createError(OtpRetrieverErrorCodes.UNSUPPORTED_PLATFORM, 'OTP Retriever is only supported on Android');
}
return yield ExpoOtpRetrieverModule_1.default.getAppHash();
});
}
/**
* Adds a listener for OTP events
* @param eventName The event to listen for
* @param listener The callback function
* @returns Subscription object that can be used to remove the listener
*/
addListener(eventName, listener) {
return emitter.addListener(eventName, listener);
}
/**
* Helper method to create standardized error objects
*/
_createError(code, message, details) {
return { code, message, details };
}
}
exports.default = new OtpRetriever();
//# sourceMappingURL=index.js.map