2013-12-20 07:39:51 +00:00
|
|
|
var passport = require('passport');
|
|
|
|
var _ = require('underscore');
|
2013-11-18 23:21:42 +00:00
|
|
|
var User = require('../models/User');
|
2013-11-14 07:29:55 +00:00
|
|
|
|
2014-01-07 22:45:42 +00:00
|
|
|
/**
|
|
|
|
* GET /login
|
|
|
|
* Login page.
|
|
|
|
*/
|
2014-01-13 09:34:54 +00:00
|
|
|
|
2014-01-07 22:45:42 +00:00
|
|
|
exports.getLogin = function(req, res) {
|
|
|
|
if (req.user) return res.redirect('/');
|
|
|
|
res.render('account/login', {
|
2014-01-28 22:41:13 +00:00
|
|
|
title: 'Login'
|
2013-11-19 18:20:50 +00:00
|
|
|
});
|
2013-11-14 07:29:55 +00:00
|
|
|
};
|
|
|
|
|
2014-01-07 22:45:42 +00:00
|
|
|
/**
|
|
|
|
* POST /login
|
|
|
|
* Sign in using email and password.
|
2014-02-07 15:21:18 +00:00
|
|
|
* @param email
|
|
|
|
* @param password
|
2014-01-07 22:45:42 +00:00
|
|
|
*/
|
2014-01-13 09:34:54 +00:00
|
|
|
|
2014-01-07 22:45:42 +00:00
|
|
|
exports.postLogin = function(req, res, next) {
|
2014-01-24 03:47:21 +00:00
|
|
|
req.assert('email', 'Email is not valid').isEmail();
|
|
|
|
req.assert('password', 'Password cannot be blank').notEmpty();
|
|
|
|
|
|
|
|
var errors = req.validationErrors();
|
|
|
|
|
|
|
|
if (errors) {
|
|
|
|
req.flash('errors', errors);
|
|
|
|
return res.redirect('/login');
|
|
|
|
}
|
|
|
|
|
2014-01-07 22:45:42 +00:00
|
|
|
passport.authenticate('local', function(err, user, info) {
|
|
|
|
if (err) return next(err);
|
2014-01-13 09:34:54 +00:00
|
|
|
|
2014-01-07 22:45:42 +00:00
|
|
|
if (!user) {
|
2014-01-24 03:47:21 +00:00
|
|
|
req.flash('errors', { msg: info.message });
|
2014-01-07 22:45:42 +00:00
|
|
|
return res.redirect('/login');
|
|
|
|
}
|
2014-01-13 09:34:54 +00:00
|
|
|
|
2014-01-07 22:45:42 +00:00
|
|
|
req.logIn(user, function(err) {
|
|
|
|
if (err) return next(err);
|
2014-02-10 16:04:36 +00:00
|
|
|
req.flash('success', { msg: 'Success! You are logged in.' });
|
2014-01-07 22:45:42 +00:00
|
|
|
return res.redirect('/');
|
|
|
|
});
|
|
|
|
})(req, res, next);
|
|
|
|
};
|
|
|
|
|
2014-02-07 15:21:18 +00:00
|
|
|
/**
|
|
|
|
* GET /logout
|
|
|
|
* Log out.
|
|
|
|
*/
|
|
|
|
|
|
|
|
exports.logout = function(req, res) {
|
|
|
|
req.logout();
|
|
|
|
res.redirect('/');
|
|
|
|
};
|
|
|
|
|
2014-01-28 22:41:13 +00:00
|
|
|
/**
|
|
|
|
* GET /signup
|
|
|
|
* Signup page.
|
|
|
|
*/
|
|
|
|
|
|
|
|
exports.getSignup = function(req, res) {
|
|
|
|
if (req.user) return res.redirect('/');
|
|
|
|
res.render('account/signup', {
|
|
|
|
title: 'Create Account'
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-01-07 22:45:42 +00:00
|
|
|
/**
|
|
|
|
* POST /signup
|
|
|
|
* Create a new local account.
|
2014-02-07 15:21:18 +00:00
|
|
|
* @param email
|
|
|
|
* @param password
|
2014-01-07 22:45:42 +00:00
|
|
|
*/
|
|
|
|
|
2014-01-13 09:34:54 +00:00
|
|
|
exports.postSignup = function(req, res, next) {
|
2014-01-24 03:39:59 +00:00
|
|
|
req.assert('email', 'Email is not valid').isEmail();
|
|
|
|
req.assert('password', 'Password must be at least 4 characters long').len(4);
|
|
|
|
req.assert('confirmPassword', 'Passwords do not match').equals(req.body.password);
|
2014-01-07 22:45:42 +00:00
|
|
|
|
2014-01-24 03:39:59 +00:00
|
|
|
var errors = req.validationErrors();
|
2014-01-07 22:45:42 +00:00
|
|
|
|
2014-01-24 03:39:59 +00:00
|
|
|
if (errors) {
|
|
|
|
req.flash('errors', errors);
|
2014-01-07 22:45:42 +00:00
|
|
|
return res.redirect('/signup');
|
|
|
|
}
|
|
|
|
|
|
|
|
var user = new User({
|
2014-01-18 06:56:55 +00:00
|
|
|
email: req.body.email,
|
2014-01-07 22:45:42 +00:00
|
|
|
password: req.body.password
|
|
|
|
});
|
|
|
|
|
|
|
|
user.save(function(err) {
|
|
|
|
if (err) {
|
|
|
|
if (err.code === 11000) {
|
2014-02-01 03:18:57 +00:00
|
|
|
req.flash('errors', { msg: 'User with that email already exists.' });
|
2014-01-07 22:45:42 +00:00
|
|
|
}
|
|
|
|
return res.redirect('/signup');
|
|
|
|
}
|
|
|
|
req.logIn(user, function(err) {
|
|
|
|
if (err) return next(err);
|
|
|
|
res.redirect('/');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-01-28 22:41:13 +00:00
|
|
|
/**
|
|
|
|
* GET /account
|
|
|
|
* Profile page.
|
|
|
|
*/
|
|
|
|
|
|
|
|
exports.getAccount = function(req, res) {
|
|
|
|
res.render('account/profile', {
|
|
|
|
title: 'Account Management'
|
|
|
|
});
|
|
|
|
};
|
2014-01-07 22:45:42 +00:00
|
|
|
|
2013-12-15 19:02:41 +00:00
|
|
|
/**
|
2014-01-07 19:13:35 +00:00
|
|
|
* POST /account/profile
|
|
|
|
* Update profile information.
|
2013-12-15 19:02:41 +00:00
|
|
|
*/
|
2014-01-28 22:41:13 +00:00
|
|
|
|
2014-01-07 19:13:35 +00:00
|
|
|
exports.postUpdateProfile = function(req, res, next) {
|
2013-12-06 04:53:14 +00:00
|
|
|
User.findById(req.user.id, function(err, user) {
|
2013-12-20 01:17:15 +00:00
|
|
|
if (err) return next(err);
|
2014-01-30 22:29:06 +00:00
|
|
|
user.email = req.body.email || '';
|
2013-12-06 04:53:14 +00:00
|
|
|
user.profile.name = req.body.name || '';
|
2013-12-06 05:25:49 +00:00
|
|
|
user.profile.gender = req.body.gender || '';
|
2013-12-06 04:53:14 +00:00
|
|
|
user.profile.location = req.body.location || '';
|
|
|
|
user.profile.website = req.body.website || '';
|
|
|
|
|
|
|
|
user.save(function(err) {
|
2013-12-20 01:17:15 +00:00
|
|
|
if (err) return next(err);
|
2014-01-28 22:41:13 +00:00
|
|
|
req.flash('success', { msg: 'Profile information updated.' });
|
2013-12-06 04:53:14 +00:00
|
|
|
res.redirect('/account');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2013-12-15 19:02:41 +00:00
|
|
|
/**
|
2014-01-07 19:13:35 +00:00
|
|
|
* POST /account/password
|
|
|
|
* Update current password.
|
2014-02-07 15:21:18 +00:00
|
|
|
* @param password
|
2013-12-15 19:02:41 +00:00
|
|
|
*/
|
2013-12-15 19:11:57 +00:00
|
|
|
|
2014-01-13 09:34:54 +00:00
|
|
|
exports.postUpdatePassword = function(req, res, next) {
|
2014-02-01 08:39:11 +00:00
|
|
|
req.assert('password', 'Password must be at least 4 characters long').len(4);
|
|
|
|
req.assert('confirmPassword', 'Passwords do not match').equals(req.body.password);
|
2013-12-15 19:11:57 +00:00
|
|
|
|
2014-02-01 08:39:11 +00:00
|
|
|
var errors = req.validationErrors();
|
|
|
|
|
|
|
|
if (errors) {
|
|
|
|
req.flash('errors', errors);
|
2013-12-13 02:47:34 +00:00
|
|
|
return res.redirect('/account');
|
|
|
|
}
|
|
|
|
|
|
|
|
User.findById(req.user.id, function(err, user) {
|
2013-12-20 19:12:29 +00:00
|
|
|
if (err) return next(err);
|
2014-01-07 23:15:14 +00:00
|
|
|
|
2013-12-13 02:47:34 +00:00
|
|
|
user.password = req.body.password;
|
2014-01-07 23:15:14 +00:00
|
|
|
|
2013-12-13 02:47:34 +00:00
|
|
|
user.save(function(err) {
|
2013-12-20 19:12:29 +00:00
|
|
|
if (err) return next(err);
|
2014-01-28 22:41:13 +00:00
|
|
|
req.flash('success', { msg: 'Password has been changed.' });
|
2013-12-13 02:47:34 +00:00
|
|
|
res.redirect('/account');
|
|
|
|
});
|
|
|
|
});
|
2013-12-06 04:53:14 +00:00
|
|
|
};
|
|
|
|
|
2013-12-15 19:02:41 +00:00
|
|
|
/**
|
|
|
|
* POST /account/delete
|
2014-01-07 19:13:35 +00:00
|
|
|
* Delete user account.
|
2014-02-07 15:21:18 +00:00
|
|
|
* @param id - User ObjectId
|
2013-12-15 19:02:41 +00:00
|
|
|
*/
|
2014-01-13 09:34:54 +00:00
|
|
|
|
2013-12-20 01:17:15 +00:00
|
|
|
exports.postDeleteAccount = function(req, res, next) {
|
2013-12-12 17:41:29 +00:00
|
|
|
User.remove({ _id: req.user.id }, function(err) {
|
2013-12-20 01:17:15 +00:00
|
|
|
if (err) return next(err);
|
2013-12-12 17:41:29 +00:00
|
|
|
req.logout();
|
|
|
|
res.redirect('/');
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2013-12-12 19:05:35 +00:00
|
|
|
/**
|
2013-12-13 05:49:46 +00:00
|
|
|
* GET /account/unlink/:provider
|
2014-01-07 23:15:14 +00:00
|
|
|
* Unlink OAuth2 provider from the current user.
|
2014-02-07 15:21:18 +00:00
|
|
|
* @param provider
|
|
|
|
* @param id - User ObjectId
|
2013-12-12 19:05:35 +00:00
|
|
|
*/
|
2014-01-13 09:34:54 +00:00
|
|
|
|
2013-12-20 01:17:15 +00:00
|
|
|
exports.getOauthUnlink = function(req, res, next) {
|
2013-12-13 05:49:46 +00:00
|
|
|
var provider = req.params.provider;
|
2013-12-13 05:27:51 +00:00
|
|
|
User.findById(req.user.id, function(err, user) {
|
2013-12-20 01:17:15 +00:00
|
|
|
if (err) return next(err);
|
2013-12-20 06:59:05 +00:00
|
|
|
|
2013-12-13 06:12:59 +00:00
|
|
|
user[provider] = undefined;
|
2014-01-07 19:35:23 +00:00
|
|
|
user.tokens = _.reject(user.tokens, function(token) { return token.kind === provider; });
|
2013-12-20 06:59:05 +00:00
|
|
|
|
2013-12-13 05:27:51 +00:00
|
|
|
user.save(function(err) {
|
2013-12-20 01:17:15 +00:00
|
|
|
if (err) return next(err);
|
2014-02-01 05:04:54 +00:00
|
|
|
req.flash('info', { msg: provider + ' account has been unlinked.' });
|
2014-01-07 22:45:42 +00:00
|
|
|
res.redirect('/account');
|
2013-12-13 05:27:51 +00:00
|
|
|
});
|
|
|
|
});
|
2013-11-14 07:29:55 +00:00
|
|
|
};
|