Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*User Model found at ./models/User*/
- var mongoose = require('mongoose');
- var crypto = require('crypto');
- var jwt = require('jsonwebtoken');
- var UserSchema = new mongoose.Schema({
- username: {type:String, lowercase:true, unique:true},
- email: {type:String, unique:true},
- hash: String,
- salt: String,
- pollsCreated:[{type:mongoose.Schema.Types.ObjectId, ref:'Post'}],
- pollsVoted:[{type:mongoose.Schema.Types.ObjectId, ref:'Post'}]
- }, {collection:'Users'});
- UserSchema.methods.setPassword = function(password){
- this.salt = crypto.randomBytes(16).toString('hex');
- this.hash = crypto.pbkdf2Sync(password, this.salt, 1000, 64).toString('hex');
- };
- UserSchema.methods.validPassword = function(password){
- var hash = crypto.pbkdf2Sync(password, this.salt, 1000, 64).toString('hex');
- return this.hash === hash;
- };
- UserSchema.methods.generateJWT = function(){
- var expiry = new Date();
- expiry.setDate(expiry.getDate() + 7);
- return jwt.sign({
- _id:this._id,
- email:this.email,
- username:this.username,
- exp: parseInt(expiry.getTime() / 1000),
- }, "R3l3ntl3$$");
- };
- mongoose.model('User', UserSchema);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement