1
0
Fork 0
This repository has been archived on 2024-02-17. You can view files and clone it, but cannot push or open issues or pull requests.
letu/models/user.js

72 lines
1.9 KiB
JavaScript

"use strict";
const crypto = require("crypto");
const privateKey = process.env.NODE_ENV === "test" ? require("../config/config_example.json") : require("../config/config.json").passwordPrivateKey;
const {
Model
} = require("sequelize");
module.exports = (sequelize, DataTypes) => {
function hash(password, email) {
let cipher = crypto.createCipheriv(
"aes-256-cbc",
privateKey,
crypto.createHash("md5").update(email).digest("base64").slice(0, 16)
);
return cipher.update(password, "utf8", "base64") + cipher.final("base64");
}
class User extends Model {
static associate(models) {
}
checkPassword(password) {
return hash(password, this.email) === this.passwordHash
}
}
User.init({
email: {
type: DataTypes.STRING,
validate: {
isEmail: true
},
primaryKey: true
},
emailVerified : {
type: DataTypes.BOOLEAN,
defaultValue: false,
allowNull: false
},
emailToken: {
type: DataTypes.STRING,
unique: true
},
firstName: {
type: DataTypes.STRING,
allowNull: false,
unique: "userFullName"
},
lastName: {
type: DataTypes.STRING,
allowNull: false,
unique: "userFullName"
},
passwordHash: {
type: DataTypes.STRING,
allowNull: false,
set(value) {
if (value)
this.setDataValue("passwordHash", hash(value, this.email));
}
},
permissions: {
type: DataTypes.INTEGER,
defaultValue: 0,
allowNull: false
}
}, {
sequelize,
modelName: "User",
});
return User;
};