From 9d8897fcccfed2fc8a33a5e96e093b63740dee5d Mon Sep 17 00:00:00 2001 From: flifloo Date: Mon, 12 Oct 2020 21:28:44 +0200 Subject: [PATCH] Set user model --- models/user.js | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 models/user.js diff --git a/models/user.js b/models/user.js new file mode 100644 index 0000000..e6b0f11 --- /dev/null +++ b/models/user.js @@ -0,0 +1,72 @@ +"use strict"; + +const crypto = require("crypto"); +const privateKey = 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; +};