Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var mongoose = require('mongoose');
- var uniqueValidator = require('mongoose-unique-validator');
- var crypto = require('crypto');
- // JWT for user
- var jwt = require('jsonwebtoken');
- // secret to sign the JWT
- var secret = require('../config').secret;
- // schema representing user data
- var UserSchema = new mongoose.Schema(
- {
- username: {
- type: String,
- lowercase: true,
- unique: true,
- required: [true, "can't be blank"],
- match: [/^[a-zA-Z0-9]+$/, 'is invalid'],
- index: true,
- },
- email: {
- type: String,
- lowercase: true,
- unique: true,
- required: [true, "can't be blank"],
- match: [/\S+@\S+\.\S+/, 'is invalid'],
- index: true,
- },
- bio: String,
- image: String,
- hash: String,
- salt: String,
- },
- {timestamps: true}
- );
- UserSchema.plugin(uniqueValidator, {message: 'is already taken.'});
- // record a salt and hash for the password
- UserSchema.methods.setPassword = function (password) {
- this.salt = crypto.randomBytes(16).toString('hex');
- this.hash = crypto
- .pbkdf2Sync(password, this.salt, 10000, 512, 'sha512')
- .toString('hex');
- };
- // hash the input password with respect to the salt and see
- // if it matches the set hash
- UserSchema.methods.validPassword = function (password) {
- var hash = crypto
- .pbkdf2Sync(password, this.salt, 10000, 512, 'sha512')
- .toString('hex');
- return this.hash === hash;
- };
- // generate a JWT
- UserSchema.methods.generateJWT = function() {
- var today = new Date();
- var exp = new Date(today);
- exp.setDate(today.getDate() + 60);
- return jwt.sign({
- id: this._id,
- username: this.username,
- exp: parseInt(exp.getTime() / 1000),
- }, secret);
- };
- // send user object to front end
- UserSchema.methods.toAuthJSON = function () {
- return {
- username: this.username,
- email: this.email,
- token: this.generateJWT(),
- bio: this.bio,
- image: this,image
- };
- };
- // register the schema with mongoose as 'User'
- mongoose.model('User', UserSchema);
Add Comment
Please, Sign In to add comment