freeCodeCamp/controllers/user.js

212 lines
4.7 KiB
JavaScript
Raw Normal View History

var mongoose = require('mongoose');
var passport = require('passport');
var _ = require('underscore');
2013-11-18 23:21:42 +00:00
var User = require('../models/User');
/**
* GET /login
* Login page.
*/
2014-01-13 09:34:54 +00:00
exports.getLogin = function(req, res) {
if (req.user) return res.redirect('/');
res.render('account/login', {
title: 'Login'
2013-11-19 18:20:50 +00:00
});
};
/**
* POST /login
* Sign in using email and password.
2014-01-13 09:34:54 +00:00
* @param {string} email
* @param {string} password
*/
2014-01-13 09:34:54 +00:00
exports.postLogin = function(req, res, next) {
2014-01-24 03:47:21 +00:00
req.assert('email', 'Email cannot be blank').notEmpty();
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');
}
passport.authenticate('local', function(err, user, info) {
if (err) return next(err);
2014-01-13 09:34:54 +00:00
if (!user) {
2014-01-24 03:47:21 +00:00
req.flash('errors', { msg: info.message });
return res.redirect('/login');
}
2014-01-13 09:34:54 +00:00
req.logIn(user, function(err) {
if (err) return next(err);
return res.redirect('/');
});
})(req, res, next);
};
/**
* GET /signup
* Signup page.
*/
exports.getSignup = function(req, res) {
if (req.user) return res.redirect('/');
res.render('account/signup', {
title: 'Create Account'
});
};
/**
* POST /signup
* Create a new local account.
2014-01-13 09:34:54 +00:00
* @param {string} email
* @param {string} password
*/
2014-01-13 09:34:54 +00:00
exports.postSignup = function(req, res, next) {
req.assert('email', 'Email cannot be blank').notEmpty();
req.assert('email', 'Email is not valid').isEmail();
req.assert('password', 'Password cannot be blank').notEmpty();
req.assert('password', 'Password must be at least 4 characters long').len(4);
req.assert('confirmPassword', 'Passwords do not match').equals(req.body.password);
var errors = req.validationErrors();
if (errors) {
req.flash('errors', errors);
return res.redirect('/signup');
}
var user = new User({
email: req.body.email,
password: req.body.password
});
user.save(function(err) {
if (err) {
if (err.code === 11000) {
req.flash('errors', { msg: 'User already exists.' });
}
return res.redirect('/signup');
}
req.logIn(user, function(err) {
if (err) return next(err);
res.redirect('/');
});
});
};
/**
* GET /account
* Profile page.
*/
exports.getAccount = function(req, res) {
res.render('account/profile', {
title: 'Account Management'
});
};
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-07 19:13:35 +00:00
exports.postUpdateProfile = function(req, res, next) {
User.findById(req.user.id, function(err, user) {
if (err) return next(err);
user.email = req.body.email || '';
user.profile.name = req.body.name || '';
user.profile.gender = req.body.gender || '';
user.profile.location = req.body.location || '';
user.profile.website = req.body.website || '';
user.save(function(err) {
if (err) return next(err);
req.flash('success', { msg: 'Profile information updated.' });
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-01-13 09:34:54 +00:00
* @param {string} 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-01-07 23:15:14 +00:00
if (!req.body.password) {
req.flash('errors', { msg: 'Password cannot be blank.' });
2013-12-15 19:11:57 +00:00
return res.redirect('/account');
}
if (req.body.password !== req.body.confirmPassword) {
req.flash('errors', { msg: 'Passwords do not match.' });
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
user.password = req.body.password;
2014-01-07 23:15:14 +00:00
user.save(function(err) {
2013-12-20 19:12:29 +00:00
if (err) return next(err);
req.flash('success', { msg: 'Password has been changed.' });
res.redirect('/account');
});
});
};
2013-12-15 19:02:41 +00:00
/**
* POST /account/delete
2014-01-07 19:13:35 +00:00
* Delete user account.
2014-01-13 09:34:54 +00:00
* @param {string} id
2013-12-15 19:02:41 +00:00
*/
2014-01-13 09:34:54 +00:00
exports.postDeleteAccount = function(req, res, next) {
2013-12-12 17:41:29 +00:00
User.remove({ _id: req.user.id }, function(err) {
if (err) return next(err);
2013-12-12 17:41:29 +00:00
req.logout();
res.redirect('/');
});
};
/**
* GET /account/unlink/:provider
2014-01-07 23:15:14 +00:00
* Unlink OAuth2 provider from the current user.
2014-01-13 09:34:54 +00:00
* @param {string} provider
* @param {string} id
*/
2014-01-13 09:34:54 +00:00
exports.getOauthUnlink = function(req, res, next) {
var provider = req.params.provider;
User.findById(req.user.id, function(err, user) {
if (err) return next(err);
2013-12-20 06:59:05 +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
user.save(function(err) {
if (err) return next(err);
res.redirect('/account');
});
});
};
/**
* GET /logout
2014-01-07 23:15:14 +00:00
* Log out.
*/
2014-01-13 09:34:54 +00:00
exports.logout = function(req, res) {
req.logout();
res.redirect('/');
2013-12-20 06:59:05 +00:00
};