Mise à jour des models mysql
This commit is contained in:
@@ -0,0 +1,160 @@
|
||||
const { DataTypes, Model } = require("sequelize");
|
||||
var crypto = require('crypto');
|
||||
var jwt = require('jsonwebtoken'),
|
||||
secret = require('../../../../config').secret,
|
||||
session_lifetime = require('../../../../config').session_lifetime;
|
||||
|
||||
const sequelize = require("../../config/sequelize");
|
||||
|
||||
class User extends Model { }
|
||||
|
||||
const UserModel = () => {
|
||||
User.init(
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.STRING(24),
|
||||
allowNull: false,
|
||||
primaryKey: true
|
||||
},
|
||||
email: {
|
||||
type: DataTypes.STRING(255),
|
||||
allowNull: false,
|
||||
unique: "email"
|
||||
},
|
||||
username: {
|
||||
type: DataTypes.STRING(255),
|
||||
allowNull: false,
|
||||
unique: "username"
|
||||
},
|
||||
firstname: {
|
||||
type: DataTypes.STRING(255),
|
||||
allowNull: true
|
||||
},
|
||||
lastname: {
|
||||
type: DataTypes.STRING(255),
|
||||
allowNull: true
|
||||
},
|
||||
bio: {
|
||||
type: DataTypes.TEXT,
|
||||
allowNull: true
|
||||
},
|
||||
image: {
|
||||
type: DataTypes.TEXT,
|
||||
allowNull: true
|
||||
},
|
||||
role: {
|
||||
type: DataTypes.STRING(255),
|
||||
allowNull: true
|
||||
},
|
||||
salt: {
|
||||
type: DataTypes.STRING(255),
|
||||
allowNull: false
|
||||
},
|
||||
hash: {
|
||||
type: DataTypes.TEXT,
|
||||
allowNull: false
|
||||
}
|
||||
},
|
||||
{
|
||||
sequelize,
|
||||
tableName: 'user',
|
||||
timestamps: true,
|
||||
indexes: [
|
||||
{
|
||||
name: "PRIMARY",
|
||||
unique: true,
|
||||
using: "BTREE",
|
||||
fields: [
|
||||
{ name: "id" },
|
||||
]
|
||||
},
|
||||
{
|
||||
name: "email",
|
||||
unique: true,
|
||||
using: "BTREE",
|
||||
fields: [
|
||||
{ name: "email" },
|
||||
]
|
||||
},
|
||||
{
|
||||
name: "username",
|
||||
unique: true,
|
||||
using: "BTREE",
|
||||
fields: [
|
||||
{ name: "username" },
|
||||
]
|
||||
},
|
||||
]
|
||||
}
|
||||
);
|
||||
|
||||
User.prototype.validPassword = function (password) {
|
||||
let hash = crypto.pbkdf2Sync(password, this.salt, 10000, 512, 'sha512').toString('hex');
|
||||
return this.hash === hash;
|
||||
};
|
||||
|
||||
User.prototype.setPassword = function (password) {
|
||||
this.salt = crypto.randomBytes(16).toString('hex');
|
||||
this.hash = crypto.pbkdf2Sync(password, this.salt, 10000, 512, 'sha512').toString('hex');
|
||||
};
|
||||
|
||||
User.prototype.generateJWT = function () {
|
||||
var today = new Date();
|
||||
var exp = new Date(today.getTime() + (session_lifetime * 1000));
|
||||
|
||||
return jwt.sign({
|
||||
id: this.id,
|
||||
username: this.username,
|
||||
exp: parseInt(exp.getTime() / 1000)
|
||||
}, secret, { algorithm: 'HS256' });
|
||||
};
|
||||
|
||||
User.prototype.toAuthJSON = function () {
|
||||
return {
|
||||
id: this.id,
|
||||
email: this.email,
|
||||
role: this.role,
|
||||
token: this.generateJWT(),
|
||||
firstname: this.firstname,
|
||||
lastname: this.lastname,
|
||||
/*phone: this.phone,
|
||||
licence: this.licence,
|
||||
poids: this.poids,*/
|
||||
username: this.username,
|
||||
image: this.image || '/assets/images/users/user.jpg',
|
||||
bg_image: this.bg_image || '/assets/images/users/bg_user.jpg'
|
||||
};
|
||||
};
|
||||
|
||||
User.prototype.toJSONFor = function () {
|
||||
return {
|
||||
id: this.id,
|
||||
email: this.email,
|
||||
username: this.username,
|
||||
firstname: this.firstname,
|
||||
lastname: this.lastname,
|
||||
bio: this.bio,
|
||||
image: this.image,
|
||||
role: this.role
|
||||
};
|
||||
};
|
||||
|
||||
User.prototype.toProfileJSONFor = function (user) {
|
||||
return {
|
||||
email: this.email,
|
||||
firstname: this.firstname,
|
||||
lastname: this.lastname,
|
||||
phone: this.phone,
|
||||
licence: this.licence,
|
||||
poids: this.poids,
|
||||
username: this.username,
|
||||
image: this.image || '/assets/images/users/user.jpg',
|
||||
bg_image: this.bg_image || '/assets/images/users/bg_user.jpg',
|
||||
following: user ? user.isFollowing(this.id) : false
|
||||
};
|
||||
};
|
||||
|
||||
return User;
|
||||
};
|
||||
|
||||
module.exports = UserModel;
|
||||
Reference in New Issue
Block a user