@fewer/password
Version:
A pipe that handles hashing passwords
27 lines • 1.15 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const bcrypt_1 = __importDefault(require("bcrypt"));
function withPassword(virtualField, hashedField, saltRounds = 12) {
return {
async use(instance) {
if (instance[virtualField]) {
// @ts-ignore: Prevent the index type from causing errors:
instance[hashedField] = await bcrypt_1.default.hash(instance[virtualField], saltRounds);
}
},
prepare(instance) {
// @ts-ignore: Prevent the index type from causing errors:
instance.authenticate = async (password) => {
if (!instance[hashedField]) {
throw new Error(`The hashed field "${hashedField}" was not present, can not authenticate`);
}
return await bcrypt_1.default.compare(password, instance[hashedField]);
};
},
};
}
exports.withPassword = withPassword;
//# sourceMappingURL=index.js.map