Guest User

Untitled

a guest
Nov 13th, 2017
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. Error: Can't set headers after they are sent.
  2. at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:356:11)
  3.  
  4. /**
  5. * GET /signup
  6. * Signup page.
  7. */
  8. exports.getSignup = (req, res) => {
  9. if (req.user) {
  10. return res.redirect('/account');
  11. }
  12. res.render('account/signup', {
  13. title: 'Create Account'
  14. });
  15. };
  16.  
  17. /**
  18. * POST /signup
  19. * Create a new local account.
  20. */
  21.  
  22. exports.postSignup = (req, res, next) => {
  23. req.assert('email', 'Email is not valid').isEmail();
  24. req.check('username', 'username must be 6-50 lower case characters, only letters and numbers, no spaces allowed.').isAlphanumeric().isLowercase().len(6);
  25. req.sanitize('email').normalizeEmail({
  26. gmail_remove_dots: false
  27. });
  28.  
  29. const errors = req.validationErrors();
  30.  
  31. if (errors) {
  32. req.flash('errors', errors);
  33. return res.redirect('/signup');
  34. }
  35.  
  36. const user = new User({
  37. email: req.body.email,
  38. username: req.body.username,
  39. // password: req.body.password
  40. });
  41.  
  42. User.findOne({
  43. email: req.body.email
  44. }, (err, existingUser) => {
  45. if (err) {
  46. return next(err);
  47. }
  48. if (existingUser) {
  49. req.flash('errors', {
  50. msg: 'Account with that email address already exists.'
  51. });
  52. return res.redirect('/signup');
  53. }
  54. });
  55.  
  56. User.findOne({
  57. username: req.body.username
  58. }, (err, existingUser) => {
  59. if (err) {
  60. return next(err);
  61. }
  62. if (existingUser) {
  63. req.flash('errors', {
  64. msg: 'Account with that username name already exists.'
  65. });
  66. return res.redirect('/signup');
  67. }
  68.  
  69. });
  70.  
  71. user.save((err) => {
  72. if (err) {
  73. return next(err);
  74. }
  75. req.logIn(user, (err) => {
  76. if (err) {
  77. return next(err);
  78. }
  79. // doesn't wokr either res.redirect('/account');
  80. return res.redirect('/account');
  81. });
  82. });
  83. };
Add Comment
Please, Sign In to add comment