From ad4a8a1cca1049f600ebcdce9260c1021cd821a5 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Sat, 18 Feb 2017 09:29:59 +0100 Subject: Add email to users --- server/models/user.js | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) (limited to 'server/models/user.js') diff --git a/server/models/user.js b/server/models/user.js index 6cb9eec3f..35a98dd6b 100644 --- a/server/models/user.js +++ b/server/models/user.js @@ -32,6 +32,13 @@ module.exports = function (sequelize, DataTypes) { } } }, + email: { + type: DataTypes.STRING, + allowNull: false, + validate: { + isEmail: true + } + }, role: { type: DataTypes.ENUM(values(constants.USER_ROLES)), allowNull: false @@ -42,6 +49,10 @@ module.exports = function (sequelize, DataTypes) { { fields: [ 'username' ], unique: true + }, + { + fields: [ 'email' ], + unique: true } ], classMethods: { @@ -52,7 +63,8 @@ module.exports = function (sequelize, DataTypes) { list, listForApi, loadById, - loadByUsername + loadByUsername, + loadByUsernameOrEmail }, instanceMethods: { isPasswordMatch, @@ -88,6 +100,7 @@ function toFormatedJSON () { return { id: this.id, username: this.username, + email: this.email, role: this.role, createdAt: this.createdAt } @@ -151,3 +164,13 @@ function loadByUsername (username, callback) { return this.findOne(query).asCallback(callback) } + +function loadByUsernameOrEmail (username, email, callback) { + const query = { + where: { + $or: [ { username }, { email } ] + } + } + + return this.findOne(query).asCallback(callback) +} -- cgit v1.2.3