Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (function() {
- var Schema, UserSchema, bcrypt, crypto, encryptPassword, mongoose, rbytes, shasum;
- crypto = require('crypto');
- shasum = crypto.createHash('sha1');
- bcrypt = require('bcrypt');
- mongoose = require('mongoose');
- Schema = mongoose.Schema;
- rbytes = require("rbytes");
- encryptPassword = function(pw) {
- var salt;
- salt = bcrypt.gen_salt_sync(10);
- return [bcrypt.encrypt_sync(pw, salt), salt];
- };
- UserSchema = new Schema({
- username: {
- type: String,
- required: true,
- unique: true
- },
- hashed_password: {
- type: String,
- required: true
- },
- password_salt: {
- type: String,
- required: true
- },
- created: {
- type: Date
- },
- updated: {
- type: Date,
- "default": Date.now
- },
- seats: [
- {
- type: Schema.ObjectId,
- ref: 'Seat'
- }
- ],
- payments: [
- {
- type: Schema.ObjectId,
- ref: 'Payment'
- }
- ],
- roles: [
- {
- type: Schema.ObjectId,
- ref: 'Role'
- }
- ]
- });
- UserSchema.virtual('password').set(function(password) {
- var arr;
- this._password = password;
- arr = encryptPassword(password);
- this.password_salt = arr[1];
- console.log(arr[1]);
- console.log(arr[0]);
- this.hashed_password = arr[0];
- });
- UserSchema.virtual('password').get(function() {
- return this._password;
- });
- UserSchema.method("authenticate", function(plainText) {
- if (bcrypt.compare_sync(plainText, this.hashed_password)) {
- console.log('valid');
- this.generateToken;
- return true;
- } else {
- return false;
- }
- });
- UserSchema.method("generateToken", function() {
- this.authentication_token = crypto.createHash("sha1").update(rbytes.randomBytes(2048)).digest("hex");
- return this.authentication_token;
- });
- UserSchema.method("tokenAuthenticate", function(token) {
- return token === this.authentication_token;
- });
- UserSchema.method("logout", function() {
- return this.authentication_token = void 0;
- });
- mongoose.model('User', UserSchema);
- module.exports = mongoose.model('User');
- }).call(this);
Add Comment
Please, Sign In to add comment