UNPKG

n8n-nodes-password-hash

Version:

Nodo personalizado para encriptar y validar contraseñas utilizando bcryptjs

120 lines 4.53 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.PasswordHash = void 0; const bcryptjs_1 = __importDefault(require("bcryptjs")); class PasswordHash { constructor() { this.description = { displayName: 'Password Hash', name: 'passwordHash', icon: 'file:passwordHash.svg', group: ['transform'], version: 1, description: 'Hash and verify passwords using bcryptjs', defaults: { name: 'PasswordHash', }, inputs: ['main'], outputs: ['main'], properties: [ { displayName: 'Operation', name: 'operation', type: 'options', noDataExpression: true, options: [ { name: 'Hash Password', value: 'hash', description: 'Generate a bcrypt hash from a password', action: 'Generate a bcrypt hash from a password', }, { name: 'Verify Password', value: 'verify', description: 'Compare a password with a bcrypt hash', action: 'Compare a password with a bcrypt hash', }, ], default: 'hash', description: 'Choose an operation', }, { displayName: 'Password', name: 'password', type: 'string', typeOptions: { password: true }, default: '', placeholder: 'MySecretPassword', description: 'The password to hash or verify', }, { displayName: 'Hash', name: 'hash', type: 'string', default: '', placeholder: '$2a$10$...', description: 'Bcrypt hash (only needed for verify)', displayOptions: { show: { operation: ['verify'], }, }, }, { displayName: 'Salt Rounds', name: 'saltRounds', type: 'number', typeOptions: { minValue: 1, maxValue: 20, }, default: 10, description: 'Number of salt rounds (only for hashing)', displayOptions: { show: { operation: ['hash'], }, }, }, ], }; } async execute() { const items = this.getInputData(); const returnData = []; for (let i = 0; i < items.length; i++) { try { const operation = this.getNodeParameter('operation', i); const password = this.getNodeParameter('password', i); if (operation === 'hash') { const saltRounds = this.getNodeParameter('saltRounds', i); const hash = await bcryptjs_1.default.hash(password, saltRounds); returnData.push({ json: { hash }, }); } else if (operation === 'verify') { const hash = this.getNodeParameter('hash', i); const match = await bcryptjs_1.default.compare(password, hash); returnData.push({ json: { match }, }); } } catch (error) { if (this.continueOnFail()) { returnData.push({ json: { error: error.message } }); continue; } throw error; } } return [returnData]; } } exports.PasswordHash = PasswordHash; //# sourceMappingURL=PasswordHash.node.js.map