- More, more, more...

This commit is contained in:
2019-07-04 23:06:04 -04:00
parent 6d5d238d34
commit af13551042
16 changed files with 348 additions and 94 deletions

View File

@@ -1,6 +1,5 @@
const mongoose = require('mongoose');
const mongooseStringQuery = require('mongoose-string-query');
const mongooseTimestamps = require('mongoose-timestamp');
const timestamps = require('mongoose-timestamp');
const AddressSchema = new mongoose.Schema(
{
@@ -38,7 +37,6 @@ const AddressSchema = new mongoose.Schema(
{ minimize: false },
);
AddressSchema.plugin(mongooseStringQuery);
AddressSchema.plugin(mongooseTimestamps);
AddressSchema.plugin(timestamps);
module.exports = AddressSchema;

View File

@@ -1,6 +1,5 @@
const mongoose = require('mongoose');
const mongooseStringQuery = require('mongoose-string-query');
const mongooseTimestamps = require('mongoose-timestamp');
const timestamps = require('mongoose-timestamp');
const EmailSchema = new mongoose.Schema(
{
@@ -24,7 +23,6 @@ EmailSchema.virtual('address').get(function() {
return this.user + '@' + this.domain;
});
EmailSchema.plugin(mongooseStringQuery);
EmailSchema.plugin(mongooseTimestamps);
EmailSchema.plugin(timestamps);
module.exports = EmailSchema;

View File

@@ -1,6 +1,5 @@
const mongoose = require('mongoose');
const mongooseStringQuery = require('mongoose-string-query');
const mongooseTimestamps = require('mongoose-timestamp');
const timestamps = require('mongoose-timestamp');
const PhoneSchema = new mongoose.Schema(
{
@@ -23,7 +22,6 @@ const PhoneSchema = new mongoose.Schema(
{ minimize: false },
);
PhoneSchema.plugin(mongooseStringQuery);
PhoneSchema.plugin(mongooseTimestamps);
PhoneSchema.plugin(timestamps);
module.exports = PhoneSchema;

View File

@@ -2,7 +2,6 @@ const { ITEM_TYPES } = require('./constants.js');
const config = require('../config.js');
const mongoose = require('mongoose');
const mongooseStringQuery = require('mongoose-string-query');
const timestamps = require('mongoose-timestamp');
const ItemSchema = new mongoose.Schema(

View File

@@ -24,6 +24,10 @@ const LoginSchema = new mongoose.Schema(
required: true,
trim: true,
},
associatedEmail: {
type: String,
trim: true,
},
secret: {
type: String,
trim: true,
@@ -66,6 +70,10 @@ const UserSchema = new mongoose.Schema(
phone: [ PhoneSchema ],
credentials: [ LoginSchema ],
tokenCheckBit: {
type: String,
trim: true,
},
organizationIdentifier: {
type: String,
@@ -88,6 +96,11 @@ const UserSchema = new mongoose.Schema(
type: Boolean,
default: false,
},
resetCheckBit: {
type: String,
default: null,
},
},
{ minimize: false },
@@ -114,6 +127,10 @@ UserSchema.methods.authenticate = function (username, password) {
return false;
};
UserSchema.methods.isNomAvailable = function (nom) {
return !!!this.model('User').findOne({ nomDeBid });
};
UserSchema.methods.generateJWT = function (props = {}) {
const { exp, iss } = props;
const today = new Date();
@@ -143,6 +160,23 @@ UserSchema.methods.getNomDeBid = function () {
return this.nomDeBid || `${this.firstName} ${this.lastName.charAt(0)}`;
};
UserSchema.methods.generateResetToken = function (callback = () => {}) {
const resetCheckBit = crypto.randomBytes(16).toString('hex');
const token = jwt.sign({
sub: this.id,
key: resetCheckBit,
iss:config.security.jwt.issuer,
aud: config.security.jwt.audience,
iat: Date.now(),
exp: (Date.now() + (24*60*60*1000)),
}, config.security.jwt.secret);
this.resetCheckBit = resetCheckBit;
this.save();
return token;
};
UserSchema.methods.isEventManager = function () {
return this.isOrganizationEmployee || false;
};
@@ -151,6 +185,28 @@ UserSchema.methods.isRegistrationVerified = function () {
return this.isVerified || false;
};
UserSchema.methods.sendPasswordReset = function () {
const resetToken = this.generateResetToken();
let resetRoute = config.security.resetRoute;
resetRoute = resetRoute.replace(':user_id', this.id);
resetRoute = resetRoute.replace(':reset_token?', resetToken);
const resetUrl = `${config.api.url}${resetRoute}`;
console.log('[sendPasswordReset] resetUrl:', resetUrl);
};
UserSchema.methods.setNomDeBid = function (nomDeBid, callback = () => {}) {
const alreadyExists = this.isNomAvailable(nomDeBid);
if (this.isNomAvailable(nomDeBid)) {
this.nomDeBid = nomDeBid;
return this.save(callback);
}
callback({ success: false, info: 'Nom de Bid already exists!' }, false);
};
UserSchema.methods.setPassword = function (password, callback = () => {}) {
const hasLocalStrategy = !!this.credentials.length &&
!!this.credentials.filter(strategy => strategy.method === 'local').length;
@@ -167,7 +223,7 @@ UserSchema.methods.setPassword = function (password, callback = () => {}) {
if (hasLocalStrategy) {
this.model('User').findOneAndUpdate(
{ _id: this._id, 'credentials.method': 'local' },
{ $set: { 'credentials.$': strategy } },
{ $set: { 'credentials.$': strategy, resetCheckBit: null } },
{ upsert: true },
callback,
);
@@ -175,6 +231,7 @@ UserSchema.methods.setPassword = function (password, callback = () => {}) {
if (!hasLocalStrategy) {
this.credentials.push(strategy);
this.resetCheckBit = null;
this.save(callback);
}
};
@@ -270,11 +327,46 @@ UserSchema.statics.findOneAndUpdateOrCreate = function (
});
};
UserSchema.statics.verifyResetToken = function (token, callback) {
jwt.verify(token, config.security.jwt.secret, (err, decoded) => {
if (err) {
return callback(err);
}
const { sub, key } = decoded;
this.findOne({ _id: sub, resetCheckBit: key }, (err, user) => {
if (err) {
return callback(err);
}
if (!user) {
return callback(err, false, 'The reset token was not valid.');
}
callback(err, user);
});
});
};
UserSchema.statics.verifyTokenAndResetPassword = function (token, password, callback) {
this.verifyResetToken(token, (err, user, info) => {
if (err) {
return callback(err);
}
if (!user) {
return callback(err, false, info);
}
user.setPassword(password, callback);
});
};
/**
* PATH OPERATIONS
*/
UserSchema.path('avatar').get(v => `${config.assetStoreUrl}${v}`);
UserSchema.path('avatar').get(v => (v ? `${config.assetStoreUrl}${v}` : null));
/**
* Export