- More, more, more...
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user