-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Sergey Peshkov
committed
Jan 29, 2020
1 parent
f082945
commit ed40ab2
Showing
3 changed files
with
202 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
module.exports = { | ||
up: (queryInterface, Sequelize) => queryInterface.createTable('users', { | ||
id: { | ||
allowNull: false, | ||
autoIncrement: true, | ||
primaryKey: true, | ||
type: Sequelize.INTEGER | ||
}, | ||
username: { | ||
type: Sequelize.STRING, | ||
allowNull: false, | ||
unique: true | ||
}, | ||
email: { | ||
type: Sequelize.STRING, | ||
allowNull: false, | ||
unique: true | ||
}, | ||
password: { | ||
type: Sequelize.STRING, | ||
allowNull: false, | ||
unique: true | ||
}, | ||
mail_confirmed_at: { | ||
type: Sequelize.DATE, | ||
allowNull: true, | ||
defaultValue: Sequelize.NOW | ||
}, | ||
active: { | ||
type: Sequelize.BOOLEAN, | ||
allowNull: true, | ||
defaultValue: true | ||
}, | ||
superadmin: { | ||
type: Sequelize.BOOLEAN, | ||
allowNull: true, | ||
defaultValue: false | ||
}, | ||
first_name: { | ||
type: Sequelize.STRING, | ||
allowNull: false | ||
}, | ||
last_name: { | ||
type: Sequelize.STRING, | ||
allowNull: false | ||
}, | ||
date_of_birth: { | ||
type: Sequelize.DATEONLY, | ||
allowNull: true | ||
}, | ||
gender: { | ||
type: Sequelize.STRING, | ||
allowNull: true | ||
}, | ||
phone: { | ||
type: Sequelize.STRING, | ||
allowNull: true | ||
}, | ||
address: { | ||
type: Sequelize.TEXT, | ||
allowNull: true | ||
}, | ||
about_me: { | ||
type: Sequelize.TEXT, | ||
allowNull: true | ||
}, | ||
created_at: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
}, | ||
updated_at: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
} | ||
}), | ||
down: (queryInterface) => { | ||
return queryInterface.dropTable('users'); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
const bcrypt = require('bcrypt'); | ||
|
||
const { Sequelize, sequelize } = require('../lib/sequelize'); | ||
const config = require('../config'); | ||
|
||
const User = sequelize.define('user', { | ||
username: { | ||
type: Sequelize.STRING, | ||
allowNull: false, | ||
defaultValue: '', | ||
validate: { | ||
notEmpty: { msg: 'Username should be set.' }, | ||
}, | ||
unique: true | ||
}, | ||
email: { | ||
type: Sequelize.STRING, | ||
allowNull: false, | ||
defaultValue: '', | ||
validate: { | ||
notEmpty: { msg: 'Email should be set.' }, | ||
isEmail: { msg: 'Email should be valid.' } | ||
}, | ||
unique: true | ||
}, | ||
password: { | ||
type: Sequelize.STRING, | ||
allowNull: false, | ||
defaultValue: '', | ||
validate: { | ||
notEmpty: { msg: 'Password should be set.' }, | ||
}, | ||
unique: true | ||
}, | ||
mail_confirmed_at: { | ||
type: Sequelize.DATE, | ||
allowNull: false, | ||
defaultValue: Sequelize.NOW | ||
}, | ||
active: { | ||
type: Sequelize.BOOLEAN, | ||
allowNull: false, | ||
defaultValue: true | ||
}, | ||
superadmin: { | ||
type: Sequelize.BOOLEAN, | ||
allowNull: false, | ||
defaultValue: false | ||
}, | ||
first_name: { | ||
type: Sequelize.STRING, | ||
allowNull: false, | ||
defaultValue: '', | ||
validate: { | ||
notEmpty: { msg: 'First name should be set.' }, | ||
} | ||
}, | ||
last_name: { | ||
type: Sequelize.STRING, | ||
allowNull: false, | ||
defaultValue: '', | ||
validate: { | ||
notEmpty: { msg: 'Last name should be set.' }, | ||
} | ||
}, | ||
date_of_birth: { | ||
type: Sequelize.DATEONLY, | ||
allowNull: true | ||
}, | ||
gender: { | ||
type: Sequelize.STRING, | ||
allowNull: true, | ||
defaultValue: '' | ||
}, | ||
phone: { | ||
type: Sequelize.STRING, | ||
allowNull: true, | ||
defaultValue: '' | ||
}, | ||
address: { | ||
type: Sequelize.TEXT, | ||
allowNull: true, | ||
defaultValue: '' | ||
}, | ||
about_me: { | ||
type: Sequelize.TEXT, | ||
allowNull: true, | ||
defaultValue: '' | ||
} | ||
}, { | ||
underscored: true, | ||
tableName: 'users', | ||
createdAt: 'created_at', | ||
updatedAt: 'updated_at', | ||
defaultScope: { | ||
attributes: { exclude: ['password'] } | ||
}, | ||
scopes: { | ||
withPassword: { | ||
attributes: { exclude: [] } | ||
}, | ||
noExtraFields: { | ||
attributes: { exclude: ['password', 'superadmin', 'active', 'mail_confirmed_at'] } | ||
} | ||
} | ||
}); | ||
|
||
User.afterValidate(async (user) => { | ||
if (user.changed('password')) { | ||
user.password = await bcrypt.hash(user.password, config.salt_rounds); | ||
} | ||
}); | ||
|
||
User.prototype.checkPassword = async function checkPassword(password) { | ||
return bcrypt.compare(password, this.password); | ||
}; | ||
|
||
module.exports = User; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const User = require('./User'); | ||
|
||
module.exports = { | ||
User | ||
}; |