UNPKG

universal_authentication

Version:

Seamless and Secure Authentication for Modern Web Applications: Easily integrate OTP-based email verification, Google OAuth, GitHub, Microsoft, and Okta login into your Node.js app. Modular, flexible, and database-agnostic, this package simplifies user au

40 lines (39 loc) 1.67 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.verifyPassword = exports.hashPassword = void 0; const crypto_1 = __importDefault(require("crypto")); const bcrypt_1 = __importDefault(require("bcrypt")); //function to hash the password using bcrypt or crypto const hashPassword = async (password, secureKey, algorithm) => { if (algorithm === "bcrypt") { const salt = bcrypt_1.default.genSaltSync(10); return await bcrypt_1.default.hash(password + secureKey, salt); } else if (algorithm === "crypto") { const hash = crypto_1.default.createHash("sha512"); hash.update(password + secureKey); return hash.digest("hex"); } else { throw new Error("The algorithm you entered is not supported, do use of bcrypt or crypto in your program."); } }; exports.hashPassword = hashPassword; // function to compare the hashed password with the provided password const verifyPassword = async (password, secureKey, hashPassword, algorithm) => { if (algorithm === "bcrypt") { return await bcrypt_1.default.compare(password + secureKey, hashPassword); } else if (algorithm === "crypto") { const hash = crypto_1.default.createHash("sha512"); hash.update(password + secureKey); return hash.digest("hex") === hashPassword; } else { throw new Error("The algorithm you entered is not supported, do use of bcrypt or crypto in your program."); } }; exports.verifyPassword = verifyPassword;