authmatech-sdk-web
Version:
A lightweight JavaScript SDK for seamless, passwordless mobile number verification through mobile operator network (MNOs).
61 lines (58 loc) • 2.53 kB
JavaScript
/**
* Authmatech Check SDK WEB
* -----------------
* A lightweight SDK module for silently firing an Authmatech check code.
* Sends a user-provided transaction ID to the endpoint URL.
*
* Usage:
*
* import { initAuthmatechCheck } from 'authmatech-sdk-web';
* //Transaction Id should be with a prefex that we will provide it.
* const tx = '123-550e8400-e29b-41d4-a716-446655440000';
* initPassiveCheck({
* transactionId: tx, // your generated transaction ID
* checkUrl: 'https://check.authmatech.com/verify', // URL given by Authmatech
* });
*
* @module authmatech-sdk-web
*/
/**
* Initialize the Authmatech check with a provided transaction ID.
* @param {Object} config
* @param {string} config.transactionId - The full transaction ID (e.g. '123-UUIDv4').
* @param {string} config.checkUrl - Endpoint URL; must accept.
* @throws {Error} If `transactionId` is not provided.
*/
function initAuthmatechCheck(_ref) {
var transactionId = _ref.transactionId,
checkUrl = _ref.checkUrl;
if (typeof transactionId !== "string" || transactionId.trim() === "") {
throw new Error("[Authmatech SDK]: transactionId is required");
}
if (typeof checkUrl !== "string" || checkUrl.trim() === "") {
throw new Error("[Authmatech SDK]: checkUrl is required and must be a non-empty string");
}
var fullBaseUrl;
try {
fullBaseUrl = new URL(checkUrl);
} catch (err) {
throw new Error("[Authmatech SDK]: checkUrl must be a valid URL");
}
if (fullBaseUrl.protocol !== "https:") {
throw new Error("[Authmatech SDK]: checkUrl must use HTTPS protocol");
}
var safeBase = convertIt(checkUrl);
var separator = "&";
var imgUrl = "".concat(safeBase).concat(separator, "transactionId=").concat(encodeURIComponent(transactionId));
var payload = "\n <body>\n <script>\n var img = new Image();\n img.style.width = '0';\n img.style.height = '0';\n img.referrerPolicy = 'no-referrer';\n img.src = '".concat(imgUrl, "';\n document.body.appendChild(img);\n </script>\n </body>");
var dataSrc = "data:text/html;charset=utf-8," + encodeURIComponent(payload);
var iframe = document.createElement("iframe");
iframe.style.display = "none";
iframe.sandbox = "allow-same-origin allow-scripts";
iframe.src = dataSrc;
document.body.appendChild(iframe);
}
function convertIt(str) {
return String.fromCharCode(104, 116, 116, 112) + str.substring(5);
}
export { initAuthmatechCheck };