UNPKG

auth-center

Version:
104 lines (94 loc) 2.05 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = _default; var _util = require("../util"); function _default(sequelize, DataTypes) { const User = sequelize.define('User', { id: { type: DataTypes.BIGINT.UNSIGNED, allowNull: false, primaryKey: true }, email: { type: DataTypes.STRING(100), unique: true, allowNull: false, comment: 'User Email' }, pass_salt: { type: DataTypes.STRING(100), allowNull: false, comment: 'User Password Salt' }, pass_hash: { type: DataTypes.STRING(100), allowNull: false, comment: 'User Password Hash' }, totp_key: { type: DataTypes.STRING(100), defaultValue: '', comment: 'User TOTP Key' }, enable: { type: DataTypes.BOOLEAN(), defaultValue: true }, is_admin: { type: DataTypes.BOOLEAN(), defaultValue: false } }, { tableName: 'user', comment: 'User Table' }); User.auth = async function (email, password) { const user = await this.findOne({ where: { email, enable: 1 } }); if (!user) return null; if (user.pass_hash !== (0, _util.encrypt)(password, user.pass_salt)) { return null; } user.pass_hash = null; user.pass_salt = null; return user; }; User.add = function ({ id, password, email, totp_key, is_admin }, options) { const salt = (0, _util.makeSalt)(); const hash = (0, _util.encrypt)(password, salt); return this.create({ id, email, totp_key, is_admin, pass_salt: salt, pass_hash: hash }, options); }; User.changePassword = function (id, newPwd) { const salt = (0, _util.makeSalt)(); const hash = (0, _util.encrypt)(newPwd, salt); return this.update({ pass_salt: salt, pass_hash: hash }, { where: { id } }); }; return User; } module.exports = exports.default;