mirror of
https://github.com/Dkunk7/crafters-coven.git
synced 2025-12-10 22:13:54 -06:00
73 lines
2.3 KiB
JavaScript
73 lines
2.3 KiB
JavaScript
const { Model, DataTypes } = require(`sequelize`);
|
|
const bcrypt = require(`bcrypt`);
|
|
|
|
const sequelize = require(`../config/connection`);
|
|
const { Hooks } = require("sequelize/types/lib/hooks");
|
|
|
|
class User extends Model {
|
|
checkPassword(loginPw) {
|
|
// set up method to run on instance data (per user) to check password
|
|
return bcrypt.compareSync(loginPw, this.password);
|
|
}
|
|
}
|
|
|
|
User.init(
|
|
{
|
|
id: {
|
|
type: DataTypes.INTEGER,
|
|
allowNull: false,
|
|
primaryKey: true,
|
|
autoIncrement: true
|
|
},
|
|
username: {
|
|
type: DataTypes.STRING,
|
|
allowNull: false,
|
|
unique: true
|
|
},
|
|
// Probably don't need an email for something small scale like this
|
|
// email: {
|
|
// type: DataTypes.STRING,
|
|
// allowNull: false,
|
|
// unique: true,
|
|
// validate: {
|
|
// isEmail: true
|
|
// }
|
|
// },
|
|
password: {
|
|
type: DataTypes.STRING,
|
|
allowNull: false,
|
|
validate: {
|
|
len: [6]
|
|
}
|
|
}
|
|
},
|
|
{
|
|
hooks: {
|
|
// set up beforeCreate lifecycle "hook" functionality
|
|
async beforeCreate(newUserData) {
|
|
newUserData.password = await bcrypt.hash(newUserData.password, 10);
|
|
return newUserData;
|
|
},
|
|
// set up beforeUpdate lifecycle "hook" functionality
|
|
async beforeUpdate(updatedUserData) {
|
|
updatedUserData.password = await bcrypt.hash(updatedUserData.password, 10);
|
|
return updatedUserData;
|
|
}
|
|
|
|
},
|
|
// TABLE CONFIGURATION OPTIONS GO HERE (https://sequelize.org/v5/manual/models-definition.html#configuration)
|
|
|
|
// pass in imported sequelize connection (the direct connection to the database)
|
|
sequelize,
|
|
// don't automatically create createdAt/updatedAt timestamp fields
|
|
timestamps: false,
|
|
// don't pluralize name of database table
|
|
freezeTableName: true,
|
|
// use underscores instead of camel-casing (ex: comment_text instead of commentText)
|
|
underscored: true,
|
|
// make it so the model name stays lowercase in the database
|
|
modelName: `user`
|
|
}
|
|
);
|
|
|
|
module.exports = User; |