diff options
| author | lowercasename <raphaelkabo@gmail.com> | 2019-07-25 16:16:04 +0100 | 
|---|---|---|
| committer | lowercasename <raphaelkabo@gmail.com> | 2019-07-25 16:16:04 +0100 | 
| commit | 930542049e40a1a99c9a0c2c349519ccddf52140 (patch) | |
| tree | ace22d1e09c409bc47743a44b8f8ca41bb8b2dd6 /models | |
First commit
Diffstat (limited to 'models')
| -rwxr-xr-x | models/Event.js | 157 | ||||
| -rwxr-xr-x | models/Log.js | 26 | ||||
| -rwxr-xr-x | models/User.js | 43 | ||||
| -rwxr-xr-x | models/passport.js | 19 | 
4 files changed, 245 insertions, 0 deletions
diff --git a/models/Event.js b/models/Event.js new file mode 100755 index 0000000..35a8f2c --- /dev/null +++ b/models/Event.js @@ -0,0 +1,157 @@ +const mongoose = require('mongoose'); + +const Attendees = new mongoose.Schema({ +	name: { +		type: String, +		trim: true +	}, +	status: { +		type: String, +		trim: true +	}, +	email: { +		type: String, +		trim: true +	} +}) + +const ReplySchema = new mongoose.Schema({ +	id: { +		type: String, +		required: true, +    unique: true, +		sparse: true +	}, +	author: { +		type: String, +		trim: true, +		required: true +	}, +	content: { +		type: String, +		trim: true, +		required: true +	}, +	timestamp: { +		type: Date, +		trim: true, +		required: true +	} +}) + +const CommentSchema = new mongoose.Schema({ +	id: { +		type: String, +		required: true, +    unique: true, +		sparse: true +	}, +	author: { +		type: String, +		trim: true, +		required: true +	}, +	content: { +		type: String, +		trim: true, +		required: true +	}, +	timestamp: { +		type: Date, +		trim: true, +		required: true +	}, +	replies: [ReplySchema] +}) + +const EventSchema = new mongoose.Schema({ +	id: { +		type: String, +		required: true, +    unique: true +	}, +	type: { +    type: String, +    trim: true, +		required: true +  }, +  name: { +    type: String, +    trim: true, +		required: true +  }, +  location: { +    type: String, +    trim: true, +		required: true +  }, +	start: { +    type: Date, +    trim: true, +		required: true +  }, +	end: { +    type: Date, +    trim: true, +		required: true +  }, +	description: { +		type: String, +		trim: true, +		required: true +	}, +	image: { +		type: String, +		trim: true +	}, +	url: { +		type: String, +		trim: true +	}, +	creatorEmail: { +		type: String, +		trim: true +	}, +	hostName: { +		type: String, +		trim: true +	}, +	viewPassword: { +    type: String, +    trim: true +  }, +	editPassword: { +    type: String, +    trim: true +  }, +	editToken: { +    type: String, +    trim: true, +		minlength: 32, +		maxlength: 32 +  }, +	usersCanAttend: { +    type: Boolean, +    trim: true, +		default: false +  }, +	showUsersList: { +    type: Boolean, +    trim: true, +		default: false +  }, +	usersCanComment: { +    type: Boolean, +    trim: true, +		default: false +  }, +	firstLoad: { +		type: Boolean, +		trim: true, +		default: true +	}, +	attendees: [Attendees], +	comments: [CommentSchema] +}); + +module.exports = mongoose.model('Event', EventSchema); diff --git a/models/Log.js b/models/Log.js new file mode 100755 index 0000000..6ed474b --- /dev/null +++ b/models/Log.js @@ -0,0 +1,26 @@ +const mongoose = require('mongoose'); + +const LogSchema = new mongoose.Schema({ +	status: { +    type: String, +    trim: true, +		required: true +  }, +	process: { +    type: String, +    trim: true, +		required: true +  }, +  message: { +    type: String, +    trim: true, +		required: true +  }, +	timestamp: { +    type: Date, +    trim: true, +		required: true +  } +}); + +module.exports = mongoose.model('Log', LogSchema);
\ No newline at end of file diff --git a/models/User.js b/models/User.js new file mode 100755 index 0000000..9391c35 --- /dev/null +++ b/models/User.js @@ -0,0 +1,43 @@ +const mongoose = require('mongoose'); +const crypto = require('crypto'); +const jwt = require('jsonwebtoken'); + +const { Schema } = mongoose; + +const UserSchema = new Schema({ +  email: String, +  hash: String, +  salt: String, +}); + +UserSchema.methods.setPassword = function(password) { +  this.salt = crypto.randomBytes(16).toString('hex'); +  this.hash = crypto.pbkdf2Sync(password, this.salt, 10000, 512, 'sha512').toString('hex'); +}; + +UserSchema.methods.validatePassword = function(password) { +  const hash = crypto.pbkdf2Sync(password, this.salt, 10000, 512, 'sha512').toString('hex'); +  return this.hash === hash; +}; + +UserSchema.methods.generateJWT = function() { +  const today = new Date(); +  const expirationDate = new Date(today); +  expirationDate.setDate(today.getDate() + 60); + +  return jwt.sign({ +    email: this.email, +    id: this._id, +    exp: parseInt(expirationDate.getTime() / 1000, 10), +  }, 'secret'); +} + +UserSchema.methods.toAuthJSON = function() { +  return { +    _id: this._id, +    email: this.email, +    token: this.generateJWT(), +  }; +}; + +mongoose.model('User', UserSchema);
\ No newline at end of file diff --git a/models/passport.js b/models/passport.js new file mode 100755 index 0000000..15020a7 --- /dev/null +++ b/models/passport.js @@ -0,0 +1,19 @@ +const mongoose = require('mongoose'); +const passport = require('passport'); +const LocalStrategy = require('passport-local'); + +const User = mongoose.model('User'); + +passport.use(new LocalStrategy({ +  usernameField: 'user[email]', +  passwordField: 'user[password]', +}, (email, password, done) => { +  User.findOne({ email }) +    .then((user) => { +      if(!user || !user.validatePassword(password)) { +        return done(null, false, { errors: { 'Email or password': 'is invalid' } }); +      } + +      return done(null, user); +    }).catch(done); +}));
\ No newline at end of file  | 
