Guest User

Untitled

a guest
Mar 16th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. function signup(req, res) {
  2. res.render('signup');
  3. }
  4.  
  5. function signupProcess(req, res) {
  6.  
  7. //we create a new User to be saved into the database
  8. var User = require('../../user/models/user');
  9. var user = new User();
  10. var utilities = require('../../core/utilities');
  11.  
  12. //we check whether all the fields were filled
  13. //a first check is performed on form but two checks are better than one...
  14. if (typeof req.body.firstName === 'string' &&
  15. typeof req.body.lastName === 'string' &&
  16. typeof req.body.username === 'string' &&
  17. typeof req.body.email === 'string' &&
  18. typeof req.body.password === 'string' &&
  19. req.body.firstName != '' &&
  20. req.body.lastName != '' &&
  21. req.body.username != '' &&
  22. req.body.email != '' &&
  23. req.body.password != '' &&
  24. utilities.validateEmail(req.body.email)) {
  25.  
  26. //we create an object with all the data
  27. user.local.firstName = req.body.firstName;
  28. user.local.lastName = req.body.lastName;
  29. user.local.username = req.body.username;
  30. user.local.email = req.body.email;
  31. user.local.password = req.body.password;
  32.  
  33. //we check whether this email already exists
  34. User.findOne({"local.email": req.body.email }, function(err, existingUser) {
  35.  
  36. if (existingUser) {
  37. res.redirect('/signup');
  38. }
  39. else {
  40. user.save(function(err, admin) {
  41. if (err) return next(err);
  42.  
  43. res.redirect('/signin');
  44. });
  45. }
  46. });
  47. }
  48.  
  49. //there are errors on the validation
  50. //issue an error to the user
  51. else {
  52. res.status(400).redirect('/signup');
  53. }
  54. }
  55.  
  56. module.exports.signup = signup;
  57. module.exports.signupProcess = signupProcess;
Add Comment
Please, Sign In to add comment