- Initial commit... A DB, some routes, and basic authentication routines...

This commit is contained in:
2019-07-04 16:19:30 -04:00
commit d9a2d33913
32 changed files with 3465 additions and 0 deletions

284
models/user.js Normal file
View File

@@ -0,0 +1,284 @@
const crypto = require('crypto');
const jwt = require('jsonwebtoken');
const mongoose = require('mongoose');
const timestamps = require('mongoose-timestamp');
const config = require('../config.js');
const AddressSchema = require('./common/address.js');
const PhoneSchema = require('./common/phone.js');
const LoginSchema = new mongoose.Schema(
{
method: {
type: String,
required: true,
enum: [ 'apple', 'facebook', 'google', 'local' ],
},
userId: {
type: String,
trim: true,
},
accessToken: {
type: String,
required: true,
trim: true,
},
secret: {
type: String,
trim: true,
},
profile: {},
},
{ minimize: false },
);
const UserSchema = new mongoose.Schema(
{
nomDeBid: {
type: String,
trim: true,
unique: true,
},
firstName: {
type: String,
required: true,
trim: true,
},
lastName: {
type: String,
required: true,
trim: true,
},
email: {
type: String,
required: true,
unique: true,
},
avatar: {
type: String,
trim: true,
},
address: [ AddressSchema ],
phone: [ PhoneSchema ],
credentials: [ LoginSchema ],
organizationIdentifier: {
type: String,
trim: true,
},
paymentToken: {
type: String,
trim: true,
},
isVerified: {
type: Boolean,
default: false,
},
isAllowedToBid: {
type: Boolean,
default: false,
},
isOrganizationEmployee: {
type: Boolean,
default: false,
},
},
{ minimize: false },
);
/**
* PLUGINS
*/
UserSchema.plugin(timestamps);
/**
* METHODS
*/
UserSchema.methods.authenticate = function (username, password) {
const user = this.model('User').findOne({ email: username });
const strategy = user ? user.getAuthStrategy('local') : null;
if (strategy) {
let hash = crypto.pbkdf2Sync(password, strategy.get('secret'), 10000, 512, 'sha512').toString('hex')
return strategy.get('accessToken') === hash;
}
return false;
};
UserSchema.methods.generateJWT = function (props = {}) {
const { exp, iss } = props;
const today = new Date();
let expirationDate = exp;
if (!expirationDate) {
expirationDate = new Date(today);
expirationDate.setDate(today.getDate() + config.security.jwt.daysValid);
}
return jwt.sign({
sub: this._id,
iss: iss || config.security.jwt.issuer,
aud: config.security.jwt.audience,
iat: parseInt(today.getTime()),
exp: parseInt(expirationDate.getTime() / 1000, 10),
}, config.security.jwt.secret);
}
UserSchema.methods.getAuthStrategy = function (method = 'local') {
return this.credentials.filter((strategy) => {
return strategy.method === method;
}).pop() || false;
};
UserSchema.methods.getNomDeBid = function () {
return this.nomDeBid || `${this.firstName} ${this.lastName.charAt(0)}`;
};
UserSchema.methods.isEventManager = function () {
return this.isOrganizationEmployee || false;
};
UserSchema.methods.isRegistrationVerified = function () {
return this.isVerified || false;
};
UserSchema.methods.setPassword = function (password, callback = () => {}) {
const hasLocalStrategy = !!this.credentials.length &&
!!this.credentials.filter(strategy => strategy.method === 'local').length;
const salt = crypto.randomBytes(16).toString('hex');
const accessToken = crypto.pbkdf2Sync(password, salt, 10000, 512, 'sha512').toString('hex');
const strategy = {
accessToken,
method: 'local',
secret: salt,
};
if (hasLocalStrategy) {
this.model('User').findOneAndUpdate(
{ _id: this._id, 'credentials.method': 'local' },
{ $set: { 'credentials.$': strategy } },
{ upsert: true },
callback,
);
}
if (!hasLocalStrategy) {
this.credentials.push(strategy);
this.save(callback);
}
};
UserSchema.methods.toAuthJSON = function () {
const hasNomDeBid = !!this.nomDeBid;
const nomDeBid = this.getNomDeBid();
return {
email: this.email,
token: this.generateJWT(),
user: {
nomDeBid: nomDeBid,
email: this.email,
firstName: this.firstName,
lastName: this.lastName,
avatar: this.avatar,
isAllowedToBid: this.isAllowedToBid,
isOrganizationEmployee: this.isOrganizationEmployee,
generatedNomDeBid: !hasNomDeBid,
},
};
};
UserSchema.methods.validatePassword = function (password) {
const strategy = this.getAuthStrategy('local');
if (strategy) {
let hash = crypto.pbkdf2Sync(password, strategy.secret, 10000, 512, 'sha512').toString('hex');
return strategy.accessToken === hash;
}
return false;
};
/**
* STATICS
*/
UserSchema.statics.findOrCreate = function (filter = {}, profile = {}, callback = () => {}) {
const self = this;
this.findOne(filter, function(err,result) {
if (err) {
callback(err, null);
}
if (!result) {
self.create(profile, (err, result) => callback(err, result));
}else{
callback(err, result);
}
});
};
UserSchema.statics.findOneAndUpdateOrCreate = function (
filter = {},
strategy = {},
profile = {},
callback = () => {},
) {
const self = this;
this.findOne(filter, function(err, result) {
if (err) {
callback(err, null);
}
if (!result) {
self.create(
{
strategy: [ strategy ],
...profile
},
(err, result) => callback(err, result),
);
} else {
const hasStrategy = !!result.credentials.length &&
!!result.credentials.filter(auth => auth.method === strategy.method).length;
if (hasStrategy) {
self.model('User').findOneAndUpdate(
{ _id: result._id, 'credentials.method': strategy.method },
{ $set: { 'credentials.$': strategy } },
{ upsert: true },
callback,
);
} else {
result.credentials.push(strategy);
result.save(callback);
}
}
});
};
/**
* PATH OPERATIONS
*/
UserSchema.path('avatar').get(v => `${config.assetStoreUrl}${v}`);
/**
* Export
*/
const User = mongoose.model('User', UserSchema);
module.exports = User;