From Tom, 1 Year ago, written in JavaScript.
Embed
  1. module.exports = function(app, urlencodedParser, User){
  2.  
  3.   app.post('/registration', urlencodedParser, function(req, res){
  4.     var reqData = {
  5.       username: req.body.username,
  6.       email: req.body.email,
  7.       password: req.body.password
  8.     };
  9.  
  10.     addUser(User, reqData, res);
  11.   });
  12. }
  13.  
  14. function addUser(User, reqData, res){
  15.   User.findOne({}, function(error, result){
  16.     if(error){
  17.       res.render('index', {status: "Server error. Please try again later"});
  18.     }
  19.     if(result.email == reqData.email){
  20.       res.render('registration', {status: "Email already in use"});
  21.     }
  22.     if(result.username == reqData.username){
  23.       res.render('registration', {status: "username already in use"});
  24.     }
  25.     else{
  26.       user = new User({
  27.         username: reqData.username,
  28.         email: reqData.email,
  29.         password: reqData.password
  30.       });
  31.       user.save(function(err){
  32.         if(err){
  33.           console.log(err);
  34.           res.render('index', {status: "Server error. Please try again later"});
  35.         }
  36.         else{
  37.           res.render('login', {status: "You are registrated. Please login"});
  38.         }
  39.       });
  40.     }
  41.   });
  42. }
  43.