1 var database = require('./database'); 2 var express = require('express'); 3 var router = express.Router(); 4 const bcrypt = require('bcryptjs'); 5 6 // const collectionName = 'Users'; 7 8 /* GET new user registration. */ 9 10 /** 11 * Function to handle GET requests for user registration. 12 * Render the registration page, and return it through res. 13 * @param {Object} req The express routing HTTP client request object, 14 * whose body contains the JSON object for user registration. 15 * @param {Object} res The express routing HTTP client response object. 16 * @param {callback} next The express routing callback function to invoke next middleware in the stack. 17 * @return {Object} A JSON object that holds req, res, and next. 18 */ 19 var registerGet = function(req, res, next) { 20 res.render('register', {err: null}); 21 } 22 23 router.get('/', registerGet); 24 25 /** 26 * Function to handle POST requests for user registration. 27 * Reject the registration iff the email, password, and confirmations fields are blank, 28 * or confirmation does not match. Otherwise add user to database. 29 * @param {Object} req The express routing HTTP client request object, 30 * whose body contains the JSON object for user registration. 31 * @param {Object} res The express routing HTTP client response object. 32 * @param {callback} next The express routing callback function to invoke next middleware in the stack. 33 * @return {Object} A JSON object that holds req, res, and next. 34 */ 35 var registerPost = function(req, res, next) { 36 let name = req.body.name; 37 let email = req.body.email; 38 let password = req.body.password; 39 let pass_confirm = req.body.password_confirmation; 40 if (name === "" || email === "" || password === "" || pass_confirm === "") { 41 res.status(401).render('register', { err: 'Error: the name, email, and passwords fields must not be blank'}); 42 return; 43 } 44 if (password !== pass_confirm) { 45 res.status(401).render('register', { err: 'Error: the passwords do not match'}); 46 return; 47 } 48 bcrypt.hash(password, 10, function(err, hash) { 49 database.insertUser(database.routerProperties(req, res, next), name, email, hash); 50 }); 51 52 53 // let db = req.app.locals.db; 54 // const collection = db.collection(collectionName); 55 // var query = {"email": email}; 56 57 // collection.find(query).toArray( 58 // function(err, result) { 59 // if (err) { 60 // next(err); 61 // return; 62 // } 63 // if (result.length !== 0) { 64 // res.status(404).send(`User with given email already exists`); 65 // return; 66 // } 67 68 // db.collection("Values").find({"name": collectionName}).toArray(function(err, resId) { 69 // if (err) { 70 // next(err); 71 // return; 72 // } 73 // let maxUserId = resId[0].maxUserId; 74 // let newUser = { 75 // userId: maxUserId, 76 // email: email, 77 // password: password, 78 // availability: [], 79 // events: [] 80 // }; 81 // collection.insertOne(newUser, function (err, insertResult) { 82 // if (err) { 83 // next(err); 84 // return; 85 // } 86 // let newValue = {$set: {"maxUserId": maxUserId + 1}}; // this is buggy 87 // db.collection("Values").updateOne({"name": "Users"}, newValue, function(err, updateResult) { 88 // if (err) { 89 // next(err); 90 // return; 91 // } 92 // res.status(201).send('Successfully inserted new user into db.'); 93 // // res.render('profile', {}); 94 // return; 95 // }); 96 // }); 97 // }); 98 // return; 99 // } 100 // ); 101 } 102 103 /* POST new user registration */ 104 router.post('/', registerPost); 105 106 module.exports = router; 107