From 9b50c108fe2ac0286c2c28ff038d9b809845bc77 Mon Sep 17 00:00:00 2001 From: Ramirez-Benmar <134592419+Ramirez-Benmar@users.noreply.github.com> Date: Thu, 3 Aug 2023 23:06:10 +0800 Subject: [PATCH 1/2] Adding Validations Moving new code, because I worked on an outdated repo --- controllers/signupController.js | 49 ++++++++--- public/js/User.js | 58 +++++++++++-- routes/routes.js | 1 + views/SignUp.hbs | 143 ++++++++++++++++++++++++++++++++ 4 files changed, 236 insertions(+), 15 deletions(-) diff --git a/controllers/signupController.js b/controllers/signupController.js index 3101df5..fdb88cb 100644 --- a/controllers/signupController.js +++ b/controllers/signupController.js @@ -12,7 +12,32 @@ const signupController = { res.render('SignUp',res); }, + checkSignUp: async function (req, res) { + const userId = req.params.idNumber + console.log(userId) + const parts = userId.split('='); + const finalId = parts[1] + console.log(typeof(finalId)) + console.log("Final ID Number is:") + console.log(finalId) + var isExisting = await User.find({'idNumber': finalId}); + console.log("Is Existing") + console.log(isExisting) + if (isExisting.length>0) { + console.log("Existing Already!") + res.json("error"); + } + else { + res.json("unique") + } + }, + postSignUp: async function (req, res) { + + console.log("Inside PostSignUp") + const { firstName, lastName, email, idNumber, password, securityCode, designation, passengerType } = req.body; + console.log(req.body) + const user = { firstName: req.body.user_firstName, lastName: req.body.user_lastName, @@ -25,18 +50,22 @@ const signupController = { profilePicture: "images/profilepictures/Default.png" } - var result = await db.insertOne(User, user); - - if( result ){ - console.log(result); - console.log('User successfully added'); - res.render('Login', {isRegistered: true}); - } - else{ - console.log('User not added'); + var isExisting = await User.findOne({'idNumber': idNumber}); + if (isExisting) { + console.log("ID NUMBER IS IN DATABASE") + res.redirect("/SignUp") + } + else { + var result = await db.insertOne(User, req.body); + if( result ){ + console.log('User successfully added'); + res.render("Login") + } + else{ + console.log('User not added'); + } } } - } module.exports = signupController; diff --git a/public/js/User.js b/public/js/User.js index fbfe2a8..2532491 100644 --- a/public/js/User.js +++ b/public/js/User.js @@ -1,10 +1,58 @@ -function showErrorBox(){ - document.getElementById('error_box').style.display = "block"; - setTimeout(hideErrorBox, 4000); -} +function showErrorBox(message) { + document.getElementById('error_box').style.display = "block"; + $("#error_message").text(message); -function hideErrorBox(){ + setTimeout(function () { document.getElementById('error_box').style.display = "none"; + }, 3000); +} + +function showSuccessBox(message) { + document.getElementById('success_box').style.display = "block"; + $("#success_message").text(message); + + setTimeout(function () { + document.getElementById('success_box').style.display = "none"; + }, 3000); +} + +async function submitForm(event) { + event.preventDefault(); + const userId = document.getElementById('user_idNumber').value; + + const response = await fetch(`/SignUp/userid=${userId}`); + const data = await response.json(); + const errorText = document.getElementById('errorText'); + + if (data === "unique") { + //SENDING POST REQUEST TO ADD INFO INTO DATABASE + //GETTING ALL DATA TO STORE IN DATABASE + const registerData = { + "firstName": document.getElementById('user_firstName').value, + "lastName": document.getElementById('user_lastName').value, + "email": document.getElementById('user_email').value, + "idNumber": document.getElementById('user_idNumber').value, + "password": document.getElementById('user_password').value, + "securityCode": document.getElementById('user_securityCode').value, + "designation": document.getElementById('user_designation').value, + "passengerType": document.getElementById('user_passengerType').value, + }; + + const response = await fetch('/SignUp', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify(registerData), + }); + if (response.ok) { + console.log('User successfully added'); + window.location.href = '/Login'; //REDIRECT TO LOGIN IF SUCCESSFUL + showSuccessBox("Account registered successfully!"); + } + } else { + showErrorBox("Data already exists!"); + } } function logoutAccount(){ diff --git a/routes/routes.js b/routes/routes.js index bc775d7..1cce1fd 100644 --- a/routes/routes.js +++ b/routes/routes.js @@ -54,6 +54,7 @@ app.post('/ChangeFPassword', forgotPassController.postChangeFPassword); // Signup settings app.get('/SignUp', signupController.getSignUp); app.post('/SignUp', signupController.postSignUp); +app.get('/SignUp/:idNumber', signupController.checkSignUp); // Security settings app.get('/SecurityCheck', securityController.getSecurity); diff --git a/views/SignUp.hbs b/views/SignUp.hbs index baabb80..759a116 100644 --- a/views/SignUp.hbs +++ b/views/SignUp.hbs @@ -8,7 +8,150 @@ + + + + + Arrow Express - Sign Up + + + + + + + + + + + + +