Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var mongoose = require('mongoose');
- var crypto = require('crypto');
- var jwt = require('jsonwebtoken');
- var config = require('../config/auth');
- var userSchema = new mongoose.Schema({
- email: {
- type: String,
- unique: true,
- required: true
- },
- username: {
- type: String,
- required: true
- },
- salt: String,
- hash: String,
- premium: Boolean,
- admin: Boolean
- });
- 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.checkPassword = 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,
- exp: parseInt(expiry.getTime() / 1000)
- }, config.secretKey);
- };
- module.exports = mongoose.model('User', userSchema);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement