Cleaned up code + simplified

This commit is contained in:
Sahat Yalkabov
2013-12-20 14:12:29 -05:00
parent 36ba87ce09
commit 30a610be2c
5 changed files with 10 additions and 16 deletions

2
app.js
View File

@ -105,5 +105,3 @@ app.get('/auth/tumblr/callback', passport.authorize('tumblr', { failureRedirect:
app.listen(app.get('port'), function() { app.listen(app.get('port'), function() {
console.log('Express server listening on port ' + app.get('port')); console.log('Express server listening on port ' + app.get('port'));
}); });
module.exports = app;

View File

@ -34,6 +34,7 @@ passport.use(new LocalStrategy(function(username, password, done) {
})); }));
passport.use(new FacebookStrategy(secrets.facebook, function (req, accessToken, refreshToken, profile, done) { passport.use(new FacebookStrategy(secrets.facebook, function (req, accessToken, refreshToken, profile, done) {
// TODO: remove duplication
if (req.user) { if (req.user) {
User.findById(req.user.id, function(err, user) { User.findById(req.user.id, function(err, user) {
user.facebook = profile.id; user.facebook = profile.id;

View File

@ -44,9 +44,9 @@ exports.postAccountProfileTab = function(req, res, next) {
* POST /account#settings * POST /account#settings
* Update user's current password * Update user's current password
*/ */
exports.postAccountSettingsTab = function(req, res) { exports.postAccountSettingsTab = function(req, res, next) {
// TODO: Virtual mongodb // TODO: Use Virtuals (mongoose)
if (!req.body.password || !req.body.confirm.password) { if (!req.body.password || !req.body.confirm.password) {
req.flash('error', 'Passwords cannot be blank'); req.flash('error', 'Passwords cannot be blank');
return res.redirect('/account'); return res.redirect('/account');
@ -58,16 +58,10 @@ exports.postAccountSettingsTab = function(req, res) {
} }
User.findById(req.user.id, function(err, user) { User.findById(req.user.id, function(err, user) {
if (err) { if (err) return next(err);
req.flash('error', err.message);
return res.redirect('/account');
}
user.password = req.body.password; user.password = req.body.password;
user.save(function(err) { user.save(function(err) {
if (err) { if (err) return next(err);
req.flash('error', err.message);
return res.redirect('/account');
}
req.flash('success', 'Password has been changed'); req.flash('success', 'Password has been changed');
res.redirect('/account'); res.redirect('/account');
}); });
@ -123,7 +117,6 @@ exports.postLogin = function(req, res, next) {
*/ */
exports.getSignup = function(req, res) { exports.getSignup = function(req, res) {
if (req.user) return res.redirect('back'); if (req.user) return res.redirect('back');
res.render('signup', { res.render('signup', {
title: 'Create Account', title: 'Create Account',
user: req.user, user: req.user,
@ -154,9 +147,11 @@ exports.postSignup = function(req, res, next) {
password: req.body.password password: req.body.password
}); });
// TODO: simplify
user.save(function(err) { user.save(function(err) {
if (err) { if (err) {
if (err.name === 'ValidationError') { if (err.name === 'ValidationError') {
// TODO: make more explicit
req.flash('messages', _.map(err.errors, function(value, key) { return value.message; })); req.flash('messages', _.map(err.errors, function(value, key) { return value.message; }));
} }
if (err.code === 11000) { if (err.code === 11000) {
@ -166,7 +161,7 @@ exports.postSignup = function(req, res, next) {
} }
req.logIn(user, function(err) { req.logIn(user, function(err) {
if (err) throw err; if (err) return next(err);
res.redirect('/'); res.redirect('/');
}); });
}); });

View File

@ -24,7 +24,7 @@ block content
| Google | Google
.form-group .form-group
label.control-label(for='username') Email label.control-label(for='username') Email
input.form-control(type='text', name='username', id='username', placeholder='Email', autofocus='') input.form-control(type='text', name='username', id='username', placeholder='Email', autofocus=true)
.form-group .form-group
label.control-label(for='username') Password label.control-label(for='username') Password
input.form-control(type='password', name='password', id='password', placeholder='Password') input.form-control(type='password', name='password', id='password', placeholder='Password')

View File

@ -11,7 +11,7 @@ block content
.form-group .form-group
label.col-sm-3.control-label(for='username') Email label.col-sm-3.control-label(for='username') Email
.col-sm-7 .col-sm-7
input.form-control(type='email', name='username', id='username', placeholder='Email', autofocus='') input.form-control(type='email', name='username', id='username', placeholder='Email', autofocus=true)
.form-group .form-group
label.col-sm-3.control-label(for='username') Password label.col-sm-3.control-label(for='username') Password
.col-sm-7 .col-sm-7