daily pastebin goal
62%
SHARE
TWEET

Untitled

a guest Sep 20th, 2016 125 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. app.post('/api/register', function(req, res) {
  2.         console.log("Registering...");
  3.         // Helper function: Returns "" if the username is valid, an error message
  4.         // otherwise.
  5.  
  6.         var validateUsername = function(username) {
  7.             username = username.trim();
  8.             if (!username) {
  9.                 return "Username: A username is required.";
  10.             }
  11.             if (username.length < 5) {
  12.                 return "Username: Username must be at least 5 characters long.";
  13.             }
  14.             if (!/^[a-zA-Z0-9]+$/i.test(username)) {
  15.                 return "Username: Username must be alphanumeric.";
  16.             }
  17.             //Username check will happen separately, since it involves a callback.
  18.             return "";
  19.         }
  20.  
  21.         // Similarly, returns an error string if there's a problem, otherwise
  22.         // returns ""
  23.         var validatePassword = function(password) {
  24.             if (password == null)
  25.                 return "Password: A Password is required.";
  26.             if (password.length < 10)
  27.                 return "Password: Password must be at least 10 characters long.";
  28.             return "";
  29.         }
  30.  
  31.         var validateName = function(name) {
  32.             if (name == null) {
  33.                 return "Name: Names are required.";
  34.             }
  35.             if (!/^[a-zA-Z]+$/.test(name)) {
  36.                 return "Name: Name is not alphabetical.";
  37.             }
  38.             return "";
  39.         }
  40.  
  41.         var validateDigits = function(uid, digitCount) {
  42.             if (uid == null) {
  43.                 return "";
  44.             }
  45.             if (uid.length != digitCount) {
  46.                 return "UID or Phone Number: Incorrect digit count.";
  47.             }
  48.             if (!/^[0-9]+$/.test(uid)) {
  49.                 return "UID or Phone Number: Not Digit String";
  50.             }
  51.             return "";
  52.         }
  53.  
  54.         var validateGradYear = function(gradYear) {
  55.             if (gradYear == null) {
  56.                 return ""; 
  57.             }
  58.             if (parseInt(gradYear) == NaN || parseInt(gradYear) < 0) {
  59.                 return "GradYear: Not a valid input."
  60.             }
  61.             return "";
  62.         }
  63.  
  64.         var validateEmail = function(email) {
  65.             if (email == null) {
  66.                 return "Email: Email is required.";
  67.             }
  68.             var re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
  69.             if (!re.test(email)) {
  70.                 return "Email: Email is invalid.";
  71.             }
  72.             return "";
  73.         }
  74.  
  75.         var validateInterests = function(interests) {
  76.             var validInterests = ["dota2", "smash4", "melee", "csgo", "league", "overwatch", "hearthstone"];
  77.             for (var i = 0; i < interests.length; i++) {
  78.                 if (!validInterests.includes(interests[i])) {
  79.                     return "Interests: Invalid interest.";
  80.                 }
  81.                 return "";
  82.             }
  83.         }
  84.        
  85.         var errorValidate = function(error) {
  86.             if (error != "") {
  87.                 res.send({ error: error });
  88.             }
  89.             return;
  90.         }
  91.  
  92.         var username = req.body.username.trim();
  93.         errorValidate(validateUsername(username));
  94.  
  95.         var usernameLower = username.toLowerCase();
  96.        
  97.         var password = req.body.password;
  98.         errorValidate(validatePassword(password));
  99.  
  100.         var firstName = req.body.firstName.trim();
  101.         errorValidate(validateName(firstName));
  102.  
  103.         var lastName = req.body.lastName.trim();
  104.         errorValidate(validateName(lastName));
  105.  
  106.         var uid = req.body.uid;
  107.         errorValidate(validateDigits(uid, 9));
  108.  
  109.         var gradYear = req.body.gradYear;
  110.         errorValidate(validateGradYear(gradYear));
  111.  
  112.         var phoneNumber = req.body.phoneNumber;
  113.         errorValidate(validateDigits(phoneNumber, 10));
  114.  
  115.         var email = req.body.email;
  116.         errorValidate(validateEmail(email));
  117.  
  118.         var interests = req.body.interests;
  119.         errorValidate(validateInterests(interests));
  120.  
  121.         console.log("Input valid...");
  122.  
  123.         User.findOne({ usernameLower: usernameLower }, function(err, user) {
  124.             if (err) {
  125.                 console.log(err);
  126.                 return res.send({ error: err });
  127.             }
  128.             if (user != null) {
  129.                 console.log(user);
  130.                 // Username exists, block it
  131.                 return res.send({ error: "Username: Username is already taken." });
  132.             }
  133.             else {
  134.                 console.log("Registration okay to go, starting");
  135.                 // Add the account.
  136.                 var user = new User({
  137.                 // console.log({
  138.                     username: username,
  139.                     usernameLower: usernameLower,
  140.                     permissions: "user",
  141.                     firstName: firstName,
  142.                     lastName: lastName,
  143.                     email: email,
  144.                     gradYear: gradYear,
  145.                     UID: uid,
  146.                     phoneNumber: phoneNumber,
  147.                     interests: interests
  148.                 });
  149.                 user.password = user.generateHash(password);
  150.                 user.save();
  151.                 // setLogin(res, username, password);
  152.                 res.cookie('username', username, { signed: true });
  153.                 res.cookie('password', auth.encrypt(password), { signed: true });
  154.                 console.log("Done with cookie");
  155.                 res.send({ status: "done" });
  156.                 console.log("Super done");
  157.             }
  158.         });
  159.     });
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top