Utilisation simultanée de mongodb et mysql
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
const { DataTypes, Model } = require("sequelize");
|
||||
const sequelize = require("../config/sequelize");
|
||||
|
||||
class Comment extends Model { }
|
||||
|
||||
const CommentModel = () => {
|
||||
Comment.init(
|
||||
{
|
||||
id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true, allowNull: false },
|
||||
body: { type: DataTypes.TEXT, allowNull: false }
|
||||
},
|
||||
{
|
||||
sequelize,
|
||||
modelName: 'Comment',
|
||||
tableName: 'comment',
|
||||
timestamps: true,
|
||||
createdAt: true,
|
||||
updatedAt: true
|
||||
}
|
||||
);
|
||||
|
||||
Comment.prototype.toJSONFor = function () {
|
||||
return {
|
||||
id: this.id,
|
||||
body: this.body
|
||||
};
|
||||
};
|
||||
|
||||
return Comment;
|
||||
};
|
||||
|
||||
module.exports = CommentModel;
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
const { DataTypes, Model } = require("sequelize");
|
||||
const sequelize = require("../config/sequelize");
|
||||
|
||||
class Tag extends Model { }
|
||||
|
||||
const TagModel = () => {
|
||||
Tag.init(
|
||||
{
|
||||
name: { type: DataTypes.STRING, primaryKey: true, allowNull: false }
|
||||
},
|
||||
{
|
||||
sequelize,
|
||||
modelName: 'Tag',
|
||||
tableName: 'tag',
|
||||
timestamps: true,
|
||||
createdAt: true,
|
||||
updatedAt: true
|
||||
}
|
||||
);
|
||||
|
||||
Tag.prototype.toJSONFor = function () {
|
||||
return {
|
||||
name: this.name
|
||||
};
|
||||
};
|
||||
|
||||
return Tag;
|
||||
};
|
||||
|
||||
module.exports = TagModel;
|
||||
|
||||
@@ -1,13 +1,18 @@
|
||||
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");
|
||||
const bcrypt = require("bcrypt");
|
||||
//const bcrypt = require("bcrypt");
|
||||
|
||||
class User extends Model { }
|
||||
|
||||
const UserModel = () => {
|
||||
User.init(
|
||||
{
|
||||
id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true, allowNull: false },
|
||||
_id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true, allowNull: false },
|
||||
email: { type: DataTypes.STRING, allowNull: false, unique: true },
|
||||
username: { type: DataTypes.STRING, allowNull: false, unique: true },
|
||||
firstname: { type: DataTypes.STRING },
|
||||
@@ -15,18 +20,20 @@ const UserModel = () => {
|
||||
bio: { type: DataTypes.TEXT, allowNull: true },
|
||||
image: { type: DataTypes.TEXT, allowNull: true },
|
||||
role: { type: DataTypes.STRING },
|
||||
password: { type: DataTypes.STRING, allowNull: false }
|
||||
salt: { type: DataTypes.STRING, allowNull: false },
|
||||
hash: { type: DataTypes.TEXT, allowNull: false }
|
||||
},
|
||||
{
|
||||
sequelize,
|
||||
modelName: 'User',
|
||||
tableName: 'user',
|
||||
timestamps: false,
|
||||
timestamps: true,
|
||||
createdAt: true,
|
||||
updatedAt: true
|
||||
}
|
||||
);
|
||||
|
||||
/*
|
||||
Model.prototype.matchPassword = async function (enteredPassword) {
|
||||
return await bcrypt.compare(enteredPassword, this.password);
|
||||
};
|
||||
@@ -40,10 +47,49 @@ const UserModel = () => {
|
||||
);
|
||||
user.password = encryptedPassword;
|
||||
});
|
||||
*/
|
||||
|
||||
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,
|
||||
id: this._id,
|
||||
email: this.email,
|
||||
username: this.username,
|
||||
firstname: this.firstname,
|
||||
@@ -54,6 +100,21 @@ const UserModel = () => {
|
||||
};
|
||||
};
|
||||
|
||||
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;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user