user_control.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. var _ = require('lodash');
  2. var async = require('async');
  3. var crypto = require('crypto');
  4. var nodemailer = require('nodemailer');
  5. var passport = require('passport');
  6. var User = require('../models/user');
  7. var secure = require('../config/secure');
  8. /********** GET / Login **************/
  9. exports.getLogin = function (req, res) {
  10. if (req.user) {
  11. return res.redirect('/');
  12. }
  13. res.render('account/login', {
  14. title: 'Login'
  15. });
  16. };
  17. /********** User GET / User URL **************/
  18. exports.getUserURL = function (req, res, next) {
  19. return User.find({ username: req.params.id }, function (err, username) {
  20. if (username) {
  21. res.render('404', { url: req.url, error: '404 Not found' });
  22. return;
  23. }
  24. return res.render('account/user', {
  25. title: profile.username,
  26. url: profile.username
  27. });
  28. });
  29. };
  30. /********** POST / Login **************/
  31. exports.postLogin = function(req, res, next) {
  32. req.assert('email', 'Email is not valid').isEmail();
  33. req.assert('password', 'Password cannot be blank').notEmpty();
  34. var errors = req.validationErrors();
  35. if (errors) {
  36. req.flash('errors', errors);
  37. return res.redirect('/login');
  38. }
  39. passport.authenticate('local', function(err, user, info) {
  40. if (err) {
  41. return next(err);
  42. }
  43. if (!user) {
  44. req.flash('errors', { msg: info.message });
  45. return res.redirect('/login');
  46. }
  47. req.logIn(user, function(err) {
  48. if (err) {
  49. return next(err);
  50. }
  51. req.flash('success', { msg: 'Success! You are logged in.' });
  52. res.redirect(req.session.returnTo || '/');
  53. });
  54. })(req, res, next);
  55. };
  56. /********** GET / Logout **************/
  57. exports.logout = function(req, res) {
  58. req.logout();
  59. res.redirect('/');
  60. };
  61. /********** GET / Register **************/
  62. exports.getSignup = function(req, res) {
  63. if (req.user) {
  64. return res.redirect('/');
  65. }
  66. res.render('account/register', {
  67. title: 'Register'
  68. });
  69. };
  70. /********** POST / Register **************/
  71. exports.postSignup = function(req, res, next) {
  72. req.assert('email', 'Email is not valid').isEmail();
  73. req.assert('password', 'Password must be at least 6 characters long').len(6);
  74. req.assert('confirmPassword', 'Passwords do not match').equals(req.body.password);
  75. var errors = req.validationErrors();
  76. if (errors) {
  77. req.flash('errors', errors);
  78. return res.redirect('/register');
  79. }
  80. var user = new User({
  81. email: req.body.email,
  82. password: req.body.password
  83. });
  84. User.findOne({ email: req.body.email }, function(err, existingUser) {
  85. if (existingUser) {
  86. req.flash('errors', { msg: 'Account with that email address already exists.' });
  87. return res.redirect('/register');
  88. }
  89. user.save(function(err) {
  90. if (err) {
  91. return next(err);
  92. }
  93. req.logIn(user, function(err) {
  94. if (err) {
  95. return next(err);
  96. }
  97. res.redirect('/');
  98. });
  99. });
  100. });
  101. };
  102. /********** GET / Account **************/
  103. exports.getAccount = function(req, res) {
  104. res.render('account/profile', {
  105. title: 'Account Management'
  106. });
  107. };
  108. /********** POST / Account **************/
  109. exports.postUpdateProfile = function(req, res, next) {
  110. User.findById(req.user.id, function(err, user) {
  111. if (err) {
  112. return next(err);
  113. }
  114. user.email = req.body.email || '';
  115. user.profile.name = req.body.name || '';
  116. user.profile.username = req.body.username || '';
  117. user.profile.gender = req.body.gender || '';
  118. user.profile.location = req.body.location || '';
  119. user.profile.website = req.body.website || '';
  120. user.profile.bio = req.body.bio || '';
  121. user.save(function(err) {
  122. if (err) {
  123. return next(err);
  124. }
  125. req.flash('success', { msg: 'Profile information updated.' });
  126. res.redirect('/account');
  127. });
  128. });
  129. };
  130. /********** POST / Account / Password **************/
  131. exports.postUpdatePassword = function(req, res, next) {
  132. req.assert('password', 'Password must be at least 4 characters long').len(4);
  133. req.assert('confirmPassword', 'Passwords do not match').equals(req.body.password);
  134. var errors = req.validationErrors();
  135. if (errors) {
  136. req.flash('errors', errors);
  137. return res.redirect('/account');
  138. }
  139. User.findById(req.user.id, function(err, user) {
  140. if (err) {
  141. return next(err);
  142. }
  143. user.password = req.body.password;
  144. user.save(function(err) {
  145. if (err) {
  146. return next(err);
  147. }
  148. req.flash('success', { msg: 'Password has been changed.' });
  149. res.redirect('/account');
  150. });
  151. });
  152. };
  153. /********** POST / Account / Delete **************/
  154. exports.postDeleteAccount = function(req, res, next) {
  155. User.remove({ _id: req.user.id }, function(err) {
  156. if (err) {
  157. return next(err);
  158. }
  159. req.logout();
  160. req.flash('info', { msg: 'Your account has been deleted.' });
  161. res.redirect('/');
  162. });
  163. };
  164. /********** POST / Account / Oauth **************/
  165. exports.getOauthUnlink = function(req, res, next) {
  166. var provider = req.params.provider;
  167. User.findById(req.user.id, function(err, user) {
  168. if (err) {
  169. return next(err);
  170. }
  171. user[provider] = undefined;
  172. user.tokens = _.reject(user.tokens, function(token) { return token.kind === provider; });
  173. user.save(function(err) {
  174. if (err) return next(err);
  175. req.flash('info', { msg: provider + ' account has been unlinked.' });
  176. res.redirect('/account');
  177. });
  178. });
  179. };
  180. /********** GET / Password / :Token **************/
  181. exports.getReset = function(req, res) {
  182. if (req.isAuthenticated()) {
  183. return res.redirect('/');
  184. }
  185. User
  186. .findOne({ resetPasswordToken: req.params.token })
  187. .where('resetPasswordExpires').gt(Date.now())
  188. .exec(function(err, user) {
  189. if (err) {
  190. return next(err);
  191. }
  192. if (!user) {
  193. req.flash('errors', { msg: 'Password reset token is invalid or has expired.' });
  194. return res.redirect('/forgot');
  195. }
  196. res.render('account/reset', {
  197. title: 'Password Reset'
  198. });
  199. });
  200. };
  201. /********** POST / Password / :Token **************/
  202. exports.postReset = function(req, res, next) {
  203. req.assert('password', 'Password must be at least 4 characters long.').len(4);
  204. req.assert('confirm', 'Passwords must match.').equals(req.body.password);
  205. var errors = req.validationErrors();
  206. if (errors) {
  207. req.flash('errors', errors);
  208. return res.redirect('back');
  209. }
  210. async.waterfall([
  211. function(done) {
  212. User
  213. .findOne({ resetPasswordToken: req.params.token })
  214. .where('resetPasswordExpires').gt(Date.now())
  215. .exec(function(err, user) {
  216. if (err) {
  217. return next(err);
  218. }
  219. if (!user) {
  220. req.flash('errors', { msg: 'Password reset token is invalid or has expired.' });
  221. return res.redirect('back');
  222. }
  223. user.password = req.body.password;
  224. user.resetPasswordToken = undefined;
  225. user.resetPasswordExpires = undefined;
  226. user.save(function(err) {
  227. if (err) {
  228. return next(err);
  229. }
  230. req.logIn(user, function(err) {
  231. done(err, user);
  232. });
  233. });
  234. });
  235. },
  236. function(user, done) {
  237. var transporter = nodemailer.createTransport({
  238. service: 'Mandrill',
  239. auth: {
  240. user: secrets.mandrill.user,
  241. pass: secrets.mandrill.password
  242. }
  243. });
  244. var mailOptions = {
  245. to: user.email,
  246. from: 'admin@juryd.com',
  247. subject: 'Your Juryd password has been changed',
  248. text: 'Hello,\n\n' +
  249. 'This is a confirmation that the password for your account ' + user.email + ' has just been changed.\n'
  250. };
  251. transporter.sendMail(mailOptions, function(err) {
  252. req.flash('success', { msg: 'Success! Your password has been changed.' });
  253. done(err);
  254. });
  255. }
  256. ], function(err) {
  257. if (err) {
  258. return next(err);
  259. }
  260. res.redirect('/');
  261. });
  262. };
  263. /********** GET / Password / Forgot **************/
  264. exports.getForgot = function(req, res) {
  265. if (req.isAuthenticated()) {
  266. return res.redirect('/');
  267. }
  268. res.render('account/forgot', {
  269. title: 'Forgot Password'
  270. });
  271. };
  272. /********** Post / Password / Forgot / Email **************/
  273. exports.postForgot = function(req, res, next) {
  274. req.assert('email', 'Please enter a valid email address.').isEmail();
  275. var errors = req.validationErrors();
  276. if (errors) {
  277. req.flash('errors', errors);
  278. return res.redirect('/forgot');
  279. }
  280. async.waterfall([
  281. function(done) {
  282. crypto.randomBytes(16, function(err, buf) {
  283. var token = buf.toString('hex');
  284. done(err, token);
  285. });
  286. },
  287. function(token, done) {
  288. User.findOne({ email: req.body.email.toLowerCase() }, function(err, user) {
  289. if (!user) {
  290. req.flash('errors', { msg: 'No account with that email address exists.' });
  291. return res.redirect('/forgot');
  292. }
  293. user.resetPasswordToken = token;
  294. user.resetPasswordExpires = Date.now() + 3600000; // 1 hour
  295. user.save(function(err) {
  296. done(err, token, user);
  297. });
  298. });
  299. },
  300. function(token, user, done) {
  301. var transporter = nodemailer.createTransport({
  302. service: 'Mandrill',
  303. auth: {
  304. user: secrets.mandrill.user,
  305. pass: secrets.mandrill.password
  306. }
  307. });
  308. var mailOptions = {
  309. to: user.email,
  310. from: 'admin@juryd.com',
  311. subject: 'Juryd - Reset your password',
  312. text: 'You are receiving this email because you (or someone else) have requested the reset of the password for your account.\n\n' +
  313. 'Please click on the following link, or paste this into your browser to complete the process:\n\n' +
  314. 'http://' + req.headers.host + '/reset/' + token + '\n\n' +
  315. 'If you did not request this, please ignore this email and your password will remain unchanged.\n'
  316. };
  317. transporter.sendMail(mailOptions, function(err) {
  318. req.flash('info', { msg: 'An e-mail has been sent to ' + user.email + ' with further instructions.' });
  319. done(err, 'done');
  320. });
  321. }
  322. ], function(err) {
  323. if (err) {
  324. return next(err);
  325. }
  326. res.redirect('/forgot');
  327. });
  328. };