- Mailer setup
This commit is contained in:
108
models/user.js
108
models/user.js
@@ -5,6 +5,7 @@ const timestamps = require('mongoose-timestamp');
|
||||
|
||||
const config = require('../config.js');
|
||||
|
||||
const UserEmails = require('../emails/user');
|
||||
const AddressSchema = require('./common/address.js');
|
||||
const PhoneSchema = require('./common/phone.js');
|
||||
|
||||
@@ -97,7 +98,7 @@ const UserSchema = new mongoose.Schema(
|
||||
default: false,
|
||||
},
|
||||
|
||||
resetCheckBit: {
|
||||
tokenCheckBit: {
|
||||
type: String,
|
||||
default: null,
|
||||
},
|
||||
@@ -127,11 +128,30 @@ UserSchema.methods.authenticate = function (username, password) {
|
||||
return false;
|
||||
};
|
||||
|
||||
UserSchema.methods.isNomAvailable = function (nom) {
|
||||
return !!!this.model('User').findOne({ nomDeBid });
|
||||
UserSchema.methods.confirmRegistration = function (callback = () => {}) {
|
||||
this.isVerified = true;
|
||||
this.tokenCheckBit = undefined;
|
||||
this.save(callback);
|
||||
};
|
||||
|
||||
UserSchema.methods.generateJWT = function (props = {}) {
|
||||
UserSchema.methods.generateAccountToken = function (callback = () => {}) {
|
||||
const tokenCheckBit = crypto.randomBytes(16).toString('hex');
|
||||
const token = jwt.sign({
|
||||
sub: this.id,
|
||||
key: tokenCheckBit,
|
||||
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.tokenCheckBit = tokenCheckBit;
|
||||
this.save();
|
||||
|
||||
return token;
|
||||
};
|
||||
|
||||
UserSchema.methods.generateLoginToken = function (props = {}) {
|
||||
const { exp, iss } = props;
|
||||
const today = new Date();
|
||||
|
||||
@@ -160,33 +180,20 @@ 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;
|
||||
};
|
||||
|
||||
UserSchema.methods.isNomAvailable = function (nom) {
|
||||
return !!!this.model('User').findOne({ nomDeBid });
|
||||
};
|
||||
|
||||
UserSchema.methods.isRegistrationVerified = function () {
|
||||
return this.isVerified || false;
|
||||
};
|
||||
|
||||
UserSchema.methods.sendPasswordReset = function () {
|
||||
const resetToken = this.generateResetToken();
|
||||
const resetToken = this.generateAccountToken();
|
||||
|
||||
let resetRoute = config.security.resetRoute;
|
||||
resetRoute = resetRoute.replace(':user_id', this.id);
|
||||
@@ -196,6 +203,19 @@ UserSchema.methods.sendPasswordReset = function () {
|
||||
console.log('[sendPasswordReset] resetUrl:', resetUrl);
|
||||
};
|
||||
|
||||
UserSchema.methods.sendConfirmationEmail = function (callback) {
|
||||
const user = {
|
||||
email: this.email,
|
||||
firstName: this.firstName,
|
||||
token: this.generateAccountToken(),
|
||||
};
|
||||
|
||||
callback = typeof callback === 'function' ? callback :
|
||||
(err, info) => console.log('[UserSchema.methods.sendConfirmationEmail]', { err, info });
|
||||
|
||||
UserEmails.confirmation(user, callback);
|
||||
};
|
||||
|
||||
UserSchema.methods.setNomDeBid = function (nomDeBid, callback = () => {}) {
|
||||
const alreadyExists = this.isNomAvailable(nomDeBid);
|
||||
|
||||
@@ -223,7 +243,7 @@ UserSchema.methods.setPassword = function (password, callback = () => {}) {
|
||||
if (hasLocalStrategy) {
|
||||
this.model('User').findOneAndUpdate(
|
||||
{ _id: this._id, 'credentials.method': 'local' },
|
||||
{ $set: { 'credentials.$': strategy, resetCheckBit: null } },
|
||||
{ $set: { 'credentials.$': strategy }, $unset: { tokenCheckBit: '' } },
|
||||
{ upsert: true },
|
||||
callback,
|
||||
);
|
||||
@@ -231,7 +251,7 @@ UserSchema.methods.setPassword = function (password, callback = () => {}) {
|
||||
|
||||
if (!hasLocalStrategy) {
|
||||
this.credentials.push(strategy);
|
||||
this.resetCheckBit = null;
|
||||
this.tokenCheckBit = undefined;
|
||||
this.save(callback);
|
||||
}
|
||||
};
|
||||
@@ -242,7 +262,7 @@ UserSchema.methods.toAuthJSON = function () {
|
||||
|
||||
return {
|
||||
email: this.email,
|
||||
token: this.generateJWT(),
|
||||
token: this.generateLoginToken(),
|
||||
user: {
|
||||
id: this._id,
|
||||
nomDeBid: nomDeBid,
|
||||
@@ -354,14 +374,32 @@ UserSchema.statics.findOneAndUpdateOrCreate = function (
|
||||
});
|
||||
};
|
||||
|
||||
UserSchema.statics.verifyResetToken = function (token, callback) {
|
||||
UserSchema.statics.register = function (user, callback = () => {}) {
|
||||
this.create(user, (err, user) => {
|
||||
if (err) {
|
||||
return callback(err, null);
|
||||
}
|
||||
|
||||
if (user) {
|
||||
user.sendConfirmationEmail((err, info) => {
|
||||
if (err) {
|
||||
return callback(err, null);
|
||||
}
|
||||
|
||||
callback(null, user);
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
UserSchema.statics.verifyAccountToken = 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) => {
|
||||
this.findOne({ _id: sub, tokenCheckBit: key }, (err, user) => {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
@@ -376,7 +414,7 @@ UserSchema.statics.verifyResetToken = function (token, callback) {
|
||||
};
|
||||
|
||||
UserSchema.statics.verifyTokenAndResetPassword = function (token, password, callback) {
|
||||
this.verifyResetToken(token, (err, user, info) => {
|
||||
this.verifyAccountToken(token, (err, user, info) => {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
@@ -389,6 +427,20 @@ UserSchema.statics.verifyTokenAndResetPassword = function (token, password, call
|
||||
});
|
||||
};
|
||||
|
||||
UserSchema.statics.verifyTokenAndConfirmRegistration = function (token, callback) {
|
||||
this.verifyAccountToken(token, (err, user, info) => {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
if (!user) {
|
||||
return callback(err, false, info);
|
||||
}
|
||||
|
||||
user.confirmRegistration(callback);
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* PATH OPERATIONS
|
||||
|
||||
Reference in New Issue
Block a user