Skip to content

Commit

Permalink
Merge branch 'lab-03-routing-start' into lab-03-routing-solved
Browse files Browse the repository at this point in the history
  • Loading branch information
mrkelly committed Aug 11, 2015
2 parents 46b1ea6 + 500549b commit 497ac3c
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 37 deletions.
3 changes: 1 addition & 2 deletions api/controllers/login.controller.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
var Q = require('q');
var Boom = require('boom');
var Bcrypt = require('bcrypt');
var db = require('../services/db');
var props = require('../properties');

Expand Down Expand Up @@ -47,7 +46,7 @@ function login (request, reply) {
}

function validate (password, userPassword) {
return Q.ninvoke(Bcrypt, 'compare', password, userPassword);
return Q(password === userPassword);
}

function setSession (request, sid, cacheObject) {
Expand Down
2 changes: 1 addition & 1 deletion api/routes/auth.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ exports.register = function (server, options, next) {
server.route([
{method: 'POST', path: login, config: {handler: loginController.login, auth: false}},
{method: 'GET', path: login, config: {handler: loginController.index, auth: false}},
{method: 'POST', path: logout, handler: logoutController.logout}
{method: 'GET', path: logout, config: {handler: logoutController.logout, auth: false}}
]);

return next();
Expand Down
16 changes: 15 additions & 1 deletion api/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,28 @@ server.views({
path: Path.join(__dirname, 'views')
});

server.register(cookie, function (err) {
server.auth.strategy('session', 'cookie', true, {
password: props.security.cookieSecret,
isSecure: false,
validateFunc: function (request, session, callback) {
cache.get(session.sid, function (err, cached) {
if (err || !cached) {
return callback(err, false);
}
return callback(null, true, cached.user);
});
}
});
});

// register the routes
server.register([
require('./routes/file.route'),
require('./routes/auth.routes'),
require('./routes/projects.routes'),
require('./routes/users.routes'),
require('./routes/index.route')

], function (err) {
if (err) console.log('Error registering routes: ' + err);
});
Expand Down
59 changes: 27 additions & 32 deletions api/services/data.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
var _ = require('lodash'),
db = require('./db'),
Q = require('q'),
bcrypt = require('bcrypt');
Q = require('q');

exports.init = init;

Expand Down Expand Up @@ -36,45 +35,41 @@ function seed() {
_.forEach(users, function (user) {
console.log("Seeding " + user.username);

bcrypt.hash(user.password, 10, function (err, hash) {
user.password = hash;
db.insert('users', user)
.then(function (newUser) {

db.insert('users', user)
.then(function (newUser) {
console.log("Created " + newUser.username);

console.log("Created " + newUser.username);
var timesheets = user.username === "admin" ? adminTimesheets: userTimesheets;

var timesheets = user.username === "admin" ? adminTimesheets: userTimesheets;
_.forEach(timesheets, function (timesheet) {
var timesheetModel = _.omit(timesheet, 'timeunits');
timesheetModel.user_id = newUser._id;

_.forEach(timesheets, function (timesheet) {
var timesheetModel = _.omit(timesheet, 'timeunits');
timesheetModel.user_id = newUser._id;
db.insert('timesheets', timesheetModel)
.then(function (newTimesheet) {

db.insert('timesheets', timesheetModel)
.then(function (newTimesheet) {
console.log("Seeding timeunits : " + timesheet.timeunits.length);

console.log("Seeding timeunits : " + timesheet.timeunits.length);
_.forEach(timesheet.timeunits, function (timeunit) {
timeunit.timesheet_id = newTimesheet._id;

_.forEach(timesheet.timeunits, function (timeunit) {
timeunit.timesheet_id = newTimesheet._id;

console.log("Attempting to seed timeunit : " + timeunit.project);
db.findOne('projects', {name: timeunit.project})
.then(function (project) {
timeunit.project_id = project._id;
return db.insert('timeunits', timeunit);
})
.then(function (newTimeunit) {
console.log("Created timeunit for " + newTimeunit.dateWorked);
})
.fail(function (err) {
console.log("Error : " + err);
});
});
console.log("Attempting to seed timeunit : " + timeunit.project);
db.findOne('projects', {name: timeunit.project})
.then(function (project) {
timeunit.project_id = project._id;
return db.insert('timeunits', timeunit);
})
.then(function (newTimeunit) {
console.log("Created timeunit for " + newTimeunit.dateWorked);
})
.fail(function (err) {
console.log("Error : " + err);
});
});
});
});
});
});
});
});
})

Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
"browserify-shim": "./config/shims.js",
"dependencies": {
"axios": "^0.5.4",
"bcrypt": "^0.8.4",
"better-console": "^0.2.4",
"boom": "^2.8.0",
"classnames": "^2.1.3",
Expand Down

0 comments on commit 497ac3c

Please sign in to comment.