UNPKG

n8n-nodes-authentica

Version:

Authentica (Balance + OTP Send/Verify) node for n8n

225 lines 10 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Authentica = void 0; const n8n_workflow_1 = require("n8n-workflow"); function isE164(phone) { return /^\+[1-9]\d{6,14}$/.test((phone !== null && phone !== void 0 ? phone : '').trim()); } function isEmail(v) { return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test((v !== null && v !== void 0 ? v : '').trim()); } async function doRequest(method, path, body, i = 0) { const creds = await this.getCredentials('authenticaApi'); const baseUrl = (creds === null || creds === void 0 ? void 0 : creds.baseUrl) || 'https://api.authentica.sa'; const options = { method, url: `${baseUrl}${path}`, headers: { 'Content-Type': 'application/json', Accept: 'application/json' }, json: true, }; if (method !== 'GET' && body) { options.body = body; } try { const response = await this.helpers.httpRequestWithAuthentication.call(this, 'authenticaApi', options); return response; } catch (error) { throw error; } } class Authentica { constructor() { this.description = { displayName: 'Authentica', name: 'authentica', icon: 'file:authentica.svg', group: ['transform'], version: 1, description: 'OTP and account balance via Authentica', defaults: { name: 'Authentica' }, inputs: ["main"], outputs: ["main"], credentials: [{ name: 'authenticaApi', required: true }], properties: [ { displayName: 'Resource', name: 'resource', type: 'options', noDataExpression: true, options: [ { name: 'Account', value: 'account' }, { name: 'OTP', value: 'otp' }, ], default: 'otp', }, { displayName: 'Operation', name: 'operation', type: 'options', noDataExpression: true, displayOptions: { show: { resource: ['otp'] } }, options: [ { name: 'Send', value: 'send', action: 'Send an OTP', description: 'Send an OTP' }, { name: 'Verify', value: 'verify', action: 'Verify an OTP', description: 'Verify an OTP' }, ], default: 'send', }, { displayName: 'Method', name: 'otpMethod', type: 'options', noDataExpression: true, options: [ { name: 'Email', value: 'email' }, { name: 'SMS', value: 'sms' }, { name: 'WhatsApp', value: 'whatsapp' }, ], default: 'sms', displayOptions: { show: { resource: ['otp'], operation: ['send'] } }, }, { displayName: 'Phone', name: 'phone', type: 'string', placeholder: '+9665XXXXXXXX', default: '', displayOptions: { show: { resource: ['otp'], operation: ['send'], otpMethod: ['sms', 'whatsapp'] } }, }, { displayName: 'Email', name: 'email', type: 'string', placeholder: 'user@example.com', default: '', displayOptions: { show: { resource: ['otp'], operation: ['send'], otpMethod: ['email'] } }, }, { displayName: 'Verify With', name: 'verifyWith', type: 'options', noDataExpression: true, options: [ { name: 'Phone', value: 'phone' }, { name: 'Email', value: 'email' }, ], default: 'phone', displayOptions: { show: { resource: ['otp'], operation: ['verify'] } }, }, { displayName: 'Phone', name: 'verifyPhone', type: 'string', placeholder: '+9665XXXXXXXX', default: '', required: true, displayOptions: { show: { resource: ['otp'], operation: ['verify'], verifyWith: ['phone'] } }, }, { displayName: 'Email', name: 'verifyEmail', type: 'string', placeholder: 'user@example.com', default: '', required: true, displayOptions: { show: { resource: ['otp'], operation: ['verify'], verifyWith: ['email'] } }, }, { displayName: 'OTP Code', name: 'otp', type: 'string', default: '', required: true, displayOptions: { show: { resource: ['otp'], operation: ['verify'] } }, }, { displayName: 'Operation', name: 'operation', type: 'options', noDataExpression: true, displayOptions: { show: { resource: ['account'] } }, options: [{ name: 'Get Balance', value: 'getBalance', action: 'Get balance' }], default: 'getBalance', }, { displayName: 'Include Raw Response', name: 'includeRaw', type: 'boolean', default: false, hint: 'Attach full API response under `raw` (for debugging).', }, ], }; } async execute() { var _a, _b, _c; const items = this.getInputData(); const returnData = []; const resource = this.getNodeParameter('resource', 0); const operation = this.getNodeParameter('operation', 0); const includeRaw = this.getNodeParameter('includeRaw', 0, false); for (let i = 0; i < items.length; i++) { try { let out = {}; let res; if (resource === 'otp' && operation === 'send') { const method = this.getNodeParameter('otpMethod', i); const phone = this.getNodeParameter('phone', i, ''); const email = this.getNodeParameter('email', i, ''); if (method !== 'email') { if (!isE164(phone)) throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Phone must be E.164, e.g. +9665XXXXXXX'); } else { if (!isEmail(email)) throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Email is not valid'); } const body = { method }; if (method === 'email') body.email = email; else body.phone = phone; res = (await doRequest.call(this, 'POST', '/api/v2/send-otp', body, i)); out = { success: true }; } if (resource === 'otp' && operation === 'verify') { const verifyWith = this.getNodeParameter('verifyWith', i); const otp = this.getNodeParameter('otp', i); const body = { otp }; if (verifyWith === 'email') { body.email = this.getNodeParameter('verifyEmail', i); if (!isEmail(body.email)) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Email is not valid'); } } else { body.phone = this.getNodeParameter('verifyPhone', i); if (!isE164(body.phone)) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Phone must be E.164, e.g. +9665XXXXXXX'); } } res = (await doRequest.call(this, 'POST', '/api/v2/verify-otp', body, i)); out = { verified: true }; } if (resource === 'account' && operation === 'getBalance') { res = (await doRequest.call(this, 'GET', '/api/v2/balance', undefined, i)); const data = (_a = res.data) !== null && _a !== void 0 ? _a : res; const balance = (_b = data === null || data === void 0 ? void 0 : data.balance) !== null && _b !== void 0 ? _b : (_c = data === null || data === void 0 ? void 0 : data.data) === null || _c === void 0 ? void 0 : _c.balance; out = { balance }; } if (includeRaw && res) out.raw = res; returnData.push({ json: out, pairedItem: { item: i } }); } catch (error) { if (this.continueOnFail()) { returnData.push({ json: { error: error.message }, pairedItem: { item: i }, }); continue; } throw new n8n_workflow_1.NodeApiError(this.getNode(), error, { itemIndex: i }); } } return [returnData]; } } exports.Authentica = Authentica; //# sourceMappingURL=Authentica.node.js.map