Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict';
- var Thing = require('../api/thing/thing.model');
- var User = require('../api/user/user.model');
- var Item = require('../api/item/item.model');
- var Calendar = require('../api/calendar/calendar.model');
- Thing.find({}).remove(function() {});
- User.find({}).remove(function() {
- User.create({
- provider: 'local',
- role: 'student',
- name: 'Student',
- email: 'student@test.com',
- password: 'test',
- pin: '0807'
- }, {
- provider: 'local',
- role: 'teacher',
- name: 'Teacher',
- email: 'teacher@test.com',
- password: 'test',
- pin: '0807'
- }, {
- provider: 'local',
- role: 'admin',
- name: 'Admin',
- email: 'admin@admin.com',
- password: 'admin',
- pin: '0807'
- }, function() {
- console.log('finished populating users');
- }, function(err) {
- console.log(err);
- });
- });
- Calendar.find({}).remove(function() {});
- console.log("Removed Calendars");
- Item.find({}).remove(function () {
- Item.create({
- calendarId: "dd7sfasd8f8sd",
- title: "title",
- description: "description",
- date: new Date(),
- checklists: [],
- attachments: [],
- status: "Not Completed",
- edit: false,
- verification: "test", //This will not be here in the long run
- verify: false
- }, {
- calendarId: "323k3k2l23lk4j4",
- title: "other",
- description: "description",
- date: new Date(),
- checklists: [],
- attachments: [],
- status: "Completed",
- edit: false,
- verification: "test", //This will not be here in the long run
- verify: false
- }, {
- calendarId: "323k3k2l23lk4j4",
- title: "title",
- description: "description",
- date: new Date(),
- checklists: [],
- attachments: [],
- status: "Verified",
- edit: false,
- verification: "test", //This will not be here in the long run
- verify: false
- }, {
- calendarId: "323k3k2l23lk4j4",
- title: "test",
- description: "description",
- date: new Date(),
- checklists: [],
- attachments: [],
- status: "Not Completed",
- edit: false,
- verification: "test", //This will not be here in the long run
- verify: false
- }, function() {
- console.log('finished populating items');
- }
- );
- });
- Express server listening on 9000, in development mode
- Done waiting!
- Running "open:server" (open) task
- Running "watch" task
- Waiting...
- { _id: 5704a4d8b414a48822cd30a6, students: [], role: 'teacher' }
- [Error: Invalid or no password]
- finished populating items
- UserSchema
- .pre('save', function(next) {
- if (!this.isNew) return next();
- console.log(this);
- if (!validatePresenceOf(this.hashedPassword) && authTypes.indexOf(this.provider) === -1) {
- next(new Error('Invalid or no password'));
- if (!validatePresenceOf(this.hashedPin))
- next(new Error('Invalid pin'));
- } else {
- next();
- }
- });
- 'use strict';
- var mongoose = require('mongoose');
- var Schema = mongoose.Schema;
- var crypto = require('crypto');
- var authTypes = ['github', 'twitter', 'facebook', 'google'];
- var Student = new Schema({
- firstName: String,
- lastName: String,
- age: Number
- });
- var UserSchema = new Schema({
- name: String,
- username: String,
- email: { type: String, lowercase: true },
- role: {
- type: String,
- default: 'teacher'
- },
- teachersEmail: { type: String, lowercase: true },
- students: [Student],
- status: String,
- hashedPassword: String,
- hashedPin: String,
- hasPassword: Boolean,
- provider: String,
- salt: String,
- pinSalt: String,
- facebook: {},
- twitter: {},
- google: {},
- github: {}
- });
- /**
- * Virtuals
- */
- UserSchema
- .virtual('password')
- .set(function(password) {
- this._password = password;
- this.salt = this.makeSalt();
- this.hashedPassword = this.encryptPassword(password);
- })
- .get(function() {
- return this._password;
- });
- UserSchema
- .virtual('pin')
- .set(function(pin) {
- this._pin = pin;
- this.pinSalt = this.makeSalt();
- this.hashedPin = this.encryptPin(pin);
- })
- .get(function() {
- return this._pin;
- });
- // Public profile information
- UserSchema
- .virtual('profile')
- .get(function() {
- return {
- 'name': this.name,
- 'role': this.role
- };
- });
- // Non-sensitive info we'll be putting in the token
- UserSchema
- .virtual('token')
- .get(function() {
- return {
- '_id': this._id,
- 'role': this.role
- };
- });
- /**
- * Validations
- */
- // Validate empty email
- UserSchema
- .path('email')
- .validate(function(email) {
- if (authTypes.indexOf(this.provider) !== -1) return true;
- return email.length;
- }, 'Email cannot be blank');
- // Validate empty password
- UserSchema
- .path('hashedPassword')
- .validate(function(hashedPassword) {
- if (authTypes.indexOf(this.provider) !== -1) return true;
- return hashedPassword.length;
- }, 'Password cannot be blank');
- // Validate empty pin
- UserSchema
- .path('hashedPin')
- .validate(function(hashedPin) {
- return hashedPin.length;
- }, 'PIN cannot be blank');
- // Validate empty pin
- UserSchema
- .path('hashedPin')
- .validate(function(hashedPin) {
- return hashedPin.length == 4;
- }, 'PIN must be 4 characters in length');
- // Validate email is not taken
- UserSchema
- .path('email')
- .validate(function(value, respond) {
- var self = this;
- this.constructor.findOne({email: value}, function(err, user) {
- if(err) throw err;
- if(user) {
- if(self.id === user.id) return respond(true);
- return respond(false);
- }
- respond(true);
- });
- }, 'The specified email address is already in use.');
- var validatePresenceOf = function(value) {
- return value && value.length;
- };
- /**
- * Pre-save hook
- */
- UserSchema
- .pre('save', function(next) {
- if (!this.isNew) return next();
- console.log(this);
- if (!validatePresenceOf(this.hashedPassword) && authTypes.indexOf(this.provider) === -1) {
- next(new Error('Invalid or no password'));
- if (!validatePresenceOf(this.hashedPin))
- next(new Error('Invalid pin'));
- } else {
- next();
- }
- });
- /**
- * Methods
- */
- UserSchema.methods = {
- /**
- * Authenticate - check if the passwords are the same
- *
- * @param {String} plainText
- * @return {Boolean}
- * @api public
- */
- authenticate: function(plainText) {
- if (this.hashedPassword) {
- return this.encryptPassword(plainText) === this.hashedPassword;
- } else {
- return !!(this.google || this.facebook);
- }
- },
- verify: function(plainText) {
- return this.encryptPin(plainText) === this.hashedPin;
- },
- /**
- * Make salt
- *
- * @return {String}
- * @api public
- */
- makeSalt: function() {
- return crypto.randomBytes(16).toString('base64');
- },
- /**
- * Encrypt password
- *
- * @param {String} password
- * @return {String}
- * @api public
- */
- encryptPassword: function(password) {
- if (!password || !this.salt) return '';
- var salt = new Buffer(this.salt, 'base64');
- return crypto.pbkdf2Sync(password, salt, 10000, 64).toString('base64');
- },
- encryptPin: function(pin) {
- if (!pin || !this.pinSalt) return '';
- var pinSalt = new Buffer(this.pinSalt, 'base64');
- return crypto.pbkdf2Sync(pin, pinSalt, 10000, 64).toString('base64');
- }
- };
- module.exports = mongoose.model('User', UserSchema);
- User.find({}).remove(function() {
- User.create({
- provider: 'local',
- role: 'student',
- name: 'Student',
- email: 'student@test.com',
- password: 'test',
- pin: '0807'
- }, function() {
- console.log('Added user');
- }, function(err) {
- console.log(err);
- });
- User.create({
- provider: 'local',
- role: 'teacher',
- name: 'Teacher',
- email: 'teacher@test.com',
- password: 'test',
- pin: '0807'
- }, function() {
- console.log('Added user');
- }, function(err) {
- console.log(err);
- });
- User.create({
- provider: 'local',
- role: 'admin',
- name: 'Admin',
- email: 'admin@admin.com',
- password: 'admin',
- pin: '0807'
- }, function() {
- console.log('Added user');
- }, function(err) {
- console.log(err);
- });
- });
- Running "watch" task
- Completed in 2.258s at Wed Apr 06 2016 00:08:34 GMT-0600 (Mountain Daylight Time) - Waiting...
- { _id: 5704a7e2d85623902a78e1fc, students: [], role: 'teacher' }
- [Error: Invalid or no password]
- { _id: 5704a7e2d85623902a78e1fe, students: [], role: 'teacher' }
- [Error: Invalid or no password]
- { _id: 5704a7e2d85623902a78e200, students: [], role: 'teacher' }
- [Error: Invalid or no password]
- finished populating items
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement