Skip to content

Commit

Permalink
use generators for users
Browse files Browse the repository at this point in the history
  • Loading branch information
madhums committed Nov 27, 2015
1 parent 60f7936 commit 18320de
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 29 deletions.
41 changes: 13 additions & 28 deletions app/controllers/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,48 +5,33 @@
*/

const mongoose = require('mongoose');
const wrap = require('co-express');
const User = mongoose.model('User');
const utils = require('../../lib/utils');

/**
* Load
*/

exports.load = function (req, res, next, id) {
const options = {
criteria: { _id : id }
};
User.load(options, function (err, user) {
if (err) return next(err);
if (!user) return next(new Error('Failed to load User ' + id));
req.profile = user;
next();
});
};
exports.load = wrap(function* (req, res, next, _id) {
const criteria = { _id };
req.profile = yield User.load({ criteria });
if (!req.profile) return next(new Error('User not found'));
next();
});

/**
* Create user
*/

exports.create = function (req, res) {
exports.create = wrap(function* (req, res) {
const user = new User(req.body);
user.provider = 'local';
user.save(function (err) {
if (err) {
return res.render('users/signup', {
errors: utils.errors(err.errors || err.message),
user: user,
title: 'Sign up'
});
}

// manually login the user once successfully signed up
req.logIn(user, function (err) {
if (err) req.flash('info', 'Sorry! We are not able to log you in!');
return res.redirect('/');
});
yield user.save();
req.logIn(user, err => {
if (err) req.flash('info', 'Sorry! We are not able to log you in!');
return res.redirect('/');
});
};
});

/**
* Show profile
Expand Down
2 changes: 1 addition & 1 deletion app/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ UserSchema.statics = {

load: function (options, cb) {
options.select = options.select || 'name username';
this.findOne(options.criteria)
return this.findOne(options.criteria)
.select(options.select)
.exec(cb);
}
Expand Down

0 comments on commit 18320de

Please sign in to comment.