Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Validations #10

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 51 additions & 20 deletions controllers/signupController.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,73 @@
const db = require('../models/db.js');

const User = require('../models/userdb.js');
// import module `bcrypt`
const bcrypt = require('bcrypt');
const saltRounds = 10;

const User = require('../models/userdb.js');


const signupController = {

getSignUp: function (req, res) {
res.render('SignUp',res);
},


checkSignUp: async function (req, res) {
console.log("INSIDE SIGN UP")
const idnumber = parseInt(req.params.idNumber);
console.log(idnumber)

const user = await User.find({idNumber: idnumber})
if(user.length > 0){
console.log(user)
res.json("Taken")
}
else{
res.json("Unique")
}

},

postSignUp: async function (req, res) {

console.log("Inside PostSignUp")
const { firstName, lastName, email, idNumber, password, securityCode, designation, passengerType } = req.body;

var newPass = await bcrypt.hash(password,saltRounds)
var newSecurityCode = await bcrypt.hash(securityCode,saltRounds)

console.log(req.body)

const user = {
firstName: req.body.user_firstName,
lastName: req.body.user_lastName,
email: req.body.user_email,
idNumber: req.body.user_idNumber,
password: await bcrypt.hash(req.body.user_password, saltRounds),
securityCode: await bcrypt.hash(req.body.user_securityCode, saltRounds),
designation: req.body.user_designation,
passengerType: req.body.user_passengerType,
firstName: firstName,
lastName: lastName,
email: email,
idNumber: idNumber,
password: newPass,
securityCode: newSecurityCode,
designation: designation,
passengerType: passengerType,
profilePicture: "images/profilepictures/Default.png"
}
console.log(user)

var isExisting = await User.findOne({'idNumber': idNumber});
if (isExisting) {
console.log("ID NUMBER IS IN DATABASE")

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');
}
else {
var result = await db.insertOne(User, user);
if( result ){
console.log('User successfully added');
res.render("Login")
}
else{
console.log('User not added');
}
}
}

}

module.exports = signupController;
62 changes: 57 additions & 5 deletions public/js/User.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,62 @@
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}`);
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(){
Expand Down
1 change: 1 addition & 0 deletions routes/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
118 changes: 83 additions & 35 deletions views/SignUp.hbs
Original file line number Diff line number Diff line change
@@ -1,27 +1,92 @@
<!DOCTYPE html>

<html>
<head>
<title> Arrow Express - Sign Up </title>
<link rel="icon" type="image/png" href="../images/DLSU_Logo.png">
<link rel="stylesheet" href="./css/FormStyle.css"> <!-- Kailangan to if may separate CSS file -->
<script src="../js/User.js"> </script>
<link rel="icon" type="image/png" href="Images/DLSU_Logo.png">
<link rel="stylesheet" href="CSS/FormStyle.css"> <!-- Kailangan to if may separate CSS file -->
<script src="js/User.js"> </script>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<style>
.input{
top: 25px;
}

.form_box{
height: 610px;
}

.form_box p{
top: -35px;
}

.toggle_btn{
top: 70px;
}

.subtext a{
position: relative;
top: 25px;
}

#error_box{
width: 380px;
height: 20px;
top: 0%;
margin: auto;
background: rgb(255, 54, 54);
padding: 5px;
position: relative;
border-radius: 20px;
display: none;
}

#error_box p{
text-align: center;
font-family: sans-serif;
font-size: 13px;
position: relative;
top: 3px;
}

#success_box{
width: 380px;
height: 20px;
top: 0%;
left: 50%;
margin: auto;
background: rgb(78, 255, 54);
padding: 5px;
position: absolute;
border-radius: 20px;
display: none;
transform: translate(-50%,-50%);
}

#success_box p{
text-align: center;
font-family: sans-serif;
font-size: 13px;
position: relative;
top: 3px;
}

</style>
<body>
<div class = "banner">

<div class="navbar">
<a href="/"><img src="../images/Leftarrow.png" class = "back_img"></a>
<a href="/"> <img src= "../images/DLSU_Logo.png" class = "website_logo"> </a>
<a href="/"><img src="Images/Leftarrow.png" class = "back_img"></a>
<a href="/"> <img src= "Images/DLSU_Logo.png" class = "website_logo"> </a>
</div>

<div class="form_box">
<h2> Sign Up </h2>
<h4> It's quick and easy </h4>
<p id="errorText"></p>
<hr>
<div class="input">
<form action="/SignUp" method="post">
<form id= "sGform" onsubmit="submitForm(event)">
<div class="input_name">
<div> <input type="text" placeholder="First Name" class="fillup name" required name="user_firstName" id="user_firstName"> </div>
<div> <input type="text" placeholder="Last Name" class="fillup name" required name="user_lastName" id="user_lastName"> </div>
Expand All @@ -48,7 +113,7 @@
<option value="Employee"> Employee </option>
</select>

<button type="submit" class="toggle_btn" onclick="create_User()"> Sign Up <style> .toggle_btn{ font-size: large; font-weight: bold;}</style></button>
<button type="submit" class="toggle_btn"> Sign Up <style> .toggle_btn{ font-size: large; font-weight: bold;}</style></button>
</form>
</div>

Expand All @@ -61,33 +126,16 @@
</div>
</div>

<div id="error_box">
<p id="error_message"> </p>
</div>

<div id="success_box">
<p id="success_message"> Account has been registered! </p>
</div>

</div>
</body>

<style>
.input{
top: 25px;
}

.form_box{
height: 610px;
}

.form_box p{
top: -35px;

}

.toggle_btn{
top: 70px;
}

.subtext a{
position: relative;
top: 25px;
}

</style>


</html>
</html>