Mise à jour de routes et models
This commit is contained in:
+56
-69
@@ -1,74 +1,61 @@
|
||||
'use strict';
|
||||
|
||||
const { DataTypes, Model } = require("sequelize");
|
||||
const sequelize = require("../util/database");
|
||||
const bcrypt = require("bcryptjs");
|
||||
|
||||
const {
|
||||
Model
|
||||
} = require('sequelize');
|
||||
module.exports = (sequelize, DataTypes) => {
|
||||
class User extends Model {
|
||||
/**
|
||||
* Helper method for defining associations.
|
||||
* This method is not a part of Sequelize lifecycle.
|
||||
* The `models/index` file will call this method automatically.
|
||||
*/
|
||||
static associate(models) {
|
||||
/* define association here */
|
||||
}
|
||||
}
|
||||
User.init({
|
||||
user_id: {type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true, allowNull: false},
|
||||
user_username: {type: DataTypes.STRING, allowNull: false},
|
||||
user_lastname: {type: DataTypes.STRING},
|
||||
user_firstname: {type: DataTypes.STRING},
|
||||
user_email: {type: DataTypes.STRING},
|
||||
user_adresse: {type: DataTypes.STRING},
|
||||
user_ville: {type: DataTypes.STRING},
|
||||
user_region: {type: DataTypes.STRING},
|
||||
user_cp: {type: DataTypes.STRING},
|
||||
user_pays: {type: DataTypes.STRING},
|
||||
user_tel: {type: DataTypes.STRING},
|
||||
user_mobile: {type: DataTypes.STRING},
|
||||
user_role: {type: DataTypes.INTEGER}
|
||||
}, {
|
||||
sequelize,
|
||||
timestamps: false,
|
||||
modelName: 'User',
|
||||
tableName: 'user',
|
||||
createdAt: true,
|
||||
updatedAt: true
|
||||
});
|
||||
/**
|
||||
* "email": "jake@jake.jake",
|
||||
"token": "jwt.token.here",
|
||||
"username": "jake",
|
||||
"bio": "I work at statefarm",
|
||||
"image": null
|
||||
*/
|
||||
|
||||
User.prototype.matchPassword = async function (enteredPassword) {
|
||||
return await bcrypt.compare(enteredPassword, this.password);
|
||||
};
|
||||
const User = sequelize.define("User", {
|
||||
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 },
|
||||
lastname: { type: DataTypes.STRING },
|
||||
bio: { type: DataTypes.TEXT, allowNull: true },
|
||||
image: { type: DataTypes.TEXT, allowNull: true },
|
||||
role: { type: DataTypes.STRING },
|
||||
password: { type: DataTypes.STRING, allowNull: false },
|
||||
createdAt: { type: DataTypes.DATE, allowNull: true },
|
||||
updatedAt: { type: DataTypes.DATE, allowNull: true }
|
||||
}, {
|
||||
sequelize,
|
||||
timestamps: false,
|
||||
modelName: 'User',
|
||||
tableName: 'user',
|
||||
createdAt: true,
|
||||
updatedAt: true
|
||||
});
|
||||
|
||||
const DEFAULT_SALT_ROUNDS = 10;
|
||||
|
||||
User.addHook("beforeCreate", async (user) => {
|
||||
const encryptedPassword = await bcrypt.hash(
|
||||
user.password,
|
||||
DEFAULT_SALT_ROUNDS
|
||||
);
|
||||
user.password = encryptedPassword;
|
||||
});
|
||||
|
||||
User.prototype.toJSONFor = function () {
|
||||
return {
|
||||
id: this.mbr_id,
|
||||
username: this.mbr_pseudo,
|
||||
lastname: this.mbr_nom,
|
||||
firstname: this.mbr_prenom,
|
||||
email: this.mbr_email,
|
||||
adresse: this.mbr_adresse,
|
||||
ville: this.mbr_ville,
|
||||
region: this.mbr_region,
|
||||
cp: this.mbr_cp,
|
||||
pays: this.mbr_pays,
|
||||
tel: this.mbr_tel,
|
||||
mobile: this.mbr_mobile,
|
||||
statut: this.mbr_statut
|
||||
};
|
||||
};
|
||||
return User;
|
||||
Model.prototype.matchPassword = async function (enteredPassword) {
|
||||
return await bcrypt.compare(enteredPassword, this.password);
|
||||
};
|
||||
|
||||
const DEFAULT_SALT_ROUNDS = 10;
|
||||
|
||||
User.addHook("beforeCreate", async (user) => {
|
||||
const encryptedPassword = await bcrypt.hash(
|
||||
user.password,
|
||||
DEFAULT_SALT_ROUNDS
|
||||
);
|
||||
user.password = encryptedPassword;
|
||||
});
|
||||
|
||||
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
|
||||
};
|
||||
};
|
||||
|
||||
module.exports = User;
|
||||
|
||||
Reference in New Issue
Block a user