@snowtop/ent-password
Version:
password datatype for ents
85 lines (84 loc) • 2.83 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.PasswordType = exports.Password = void 0;
const schema_1 = require("@snowtop/ent/schema");
const bcrypt = __importStar(require("bcryptjs"));
class Password extends schema_1.BaseField {
constructor() {
super();
this.type = { dbType: schema_1.DBType.String };
// hardcode password to be private and hidden from graphql by default
this.private = true;
this.hideFromGraphQL = true;
// indicate sensitive so that we don't log the value of the password
this.sensitive = true;
this.stringType = (0, schema_1.StringType)();
}
cost(cost) {
this._cost = cost;
return this;
}
minLen(length) {
this.stringType.minLen(length);
return this;
}
maxLen(length) {
this.stringType.maxLen(length);
return this;
}
length(length) {
this.stringType.length(length);
return this;
}
match(pattern) {
this.stringType.match(pattern);
return this;
}
doesNotMatch(pattern) {
this.stringType.doesNotMatch(pattern);
return this;
}
validate(validator) {
this.stringType.validate(validator);
return this;
}
async format(val) {
// apply any string based formatting
val = this.stringType.format(val);
let salt = await bcrypt.genSalt(this._cost);
return await bcrypt.hash(val, salt);
}
valid(val) {
// default to string validation
return this.stringType.valid(val);
}
}
exports.Password = Password;
function PasswordType(options) {
let result = new Password();
return Object.assign(result, options);
}
exports.PasswordType = PasswordType;