Skip to content

Commit

Permalink
Log-in and Signup API completed
Browse files Browse the repository at this point in the history
  • Loading branch information
Batsuuri77 committed May 26, 2024
1 parent ad99222 commit a181f08
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 116 deletions.
164 changes: 78 additions & 86 deletions client/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,80 +7,54 @@ $(document).ready(function () {

localStorage.removeItem("allUsers");

if (!localStorage.allUsers) {
// if (!localStorage.allUsers) {

if (debug) alert("Users not found - creating a default user!");
// if (debug) alert("Users not found - creating a default user!");

var userData = {email:"[email protected]",password:"admin",firstName:"CQU",lastName:"User",state:"QLD",phoneNumber:"0422919919", address:"700 Yamba Road", postcode:"4701"};
// var userData = {email:"admin@domain.com",password:"admin",firstName:"CQU",lastName:"User",state:"QLD",phoneNumber:"0422919919", address:"700 Yamba Road", postcode:"4701"};

var allUsers = [];
allUsers.push(userData);
// var allUsers = [];
// allUsers.push(userData);

if (debug) alert(JSON.stringify(allUsers));
localStorage.setItem("allUsers", JSON.stringify(allUsers));
// if (debug) alert(JSON.stringify(allUsers));
// localStorage.setItem("allUsers", JSON.stringify(allUsers));

} else {
// } else {

if (debug) alert("Names Array found-loading..");
// if (debug) alert("Names Array found-loading..");

var allUsers = JSON.parse(localStorage.allUsers);
if (debug) alert(JSON.stringify(allUsers));
}
// var allUsers = JSON.parse(localStorage.allUsers);
// if (debug) alert(JSON.stringify(allUsers));
// }

if (!localStorage.allCards) {
// if (!localStorage.allCards) {

if (debug) alert("Cards not found");
// if (debug) alert("Cards not found");

var cardData = {question:"What is my name", answer:"Vinay"};
// var cardData = {question:"What is my name", answer:"Vinay"};

var allCards = [];
allCards.push(cardData);
// var allCards = [];
// allCards.push(cardData);

if (debug) alert(JSON.stringify(allCards));
localStorage.setItem("allCards", JSON.stringify(allCards));
// if (debug) alert(JSON.stringify(allCards));
// localStorage.setItem("allCards", JSON.stringify(allCards));

} else {
// } else {

if (debug) alert("Names Array found-loading..");
// if (debug) alert("Names Array found-loading..");

var allCards = JSON.parse(localStorage.allCards);
if (debug) alert(JSON.stringify(allCards));
}
// var allCards = JSON.parse(localStorage.allCards);
// if (debug) alert(JSON.stringify(allCards));
// }


/**
----------------------Event handler to process login request----------------------
**/

$('#loginButton').click(function () {

localStorage.removeItem("inputData")

$("#loginForm").submit();

if (localStorage.inputData != null) {

var inputData = JSON.parse(localStorage.getItem("inputData"));

$.post("http://localhost:3000/verifyUser", inputData, function(data, status){
if (debug) alert("Data received: " + JSON.stringify(data));
if (debug) alert("\nStatus: " + status);

if (data.length > 0) {
alert("Login success");
authenticated = true;
localStorage.setItem("userInfo", JSON.stringify(data[0]));
$.mobile.changePage("#homePage");
}
else {
alert("Login failed");
}

$("#loginForm").trigger('reset');
})
}

})
});


$("#loginForm").validate({ // JQuery validation plugin
Expand All @@ -97,6 +71,22 @@ $(document).ready(function () {

localStorage.setItem("inputData", JSON.stringify(inputData));

$.post("http://localhost:3000/verifyUser", inputData, function(data, status) {
if (debug) alert("Data received: " + JSON.stringify(data));
if (debug) alert("\nStatus: " + status);

if (data.length > 0) {
alert("Login success");
localStorage.setItem("userInfo", JSON.stringify(data[0]));
$.mobile.changePage("#homePage");
} else {
alert("Login failed: Incorrect email or password");
}

$("#loginForm").trigger('reset');
}).fail(function() {
alert("An error occurred while verifying the user. Please try again later.");
});
},
/* Validation rules */
rules: {
Expand Down Expand Up @@ -127,42 +117,47 @@ $(document).ready(function () {
--------------------------end--------------------------
**/
// Event handler for Sign-up form submission
$('#signupForm').submit(function (e) {
e.preventDefault(); // Prevent default form submission

// Validate the form using jQuery Validation plugin
if ($(this).valid()) {
var inputData = {
email: $('#email').val(),
password: $('#password').val(),
firstName: $('#firstName').val(),
lastName: $('#lastName').val(),
state: $('#state').val(),
phoneNumber: $('#phoneNumber').val(),
address: $('#address').val(),
postcode: $('#postcode').val()
};

var allUsers = JSON.parse(localStorage.getItem("allUsers")) || [];
var existingUser = allUsers.find(user => user.email === inputData.email);

if (existingUser) {
alert("User already exists! Please use a different email.");
} else {
allUsers.push(inputData);
localStorage.setItem("allUsers", JSON.stringify(allUsers));
alert("Sign up successful! You can now log in.");
$.mobile.changePage("#loginPage");
}
loadProfile();
$("#signupForm")[0].reset(); // Reset the form
}
$('#signupButton').on('click', () => {
$("#signupForm").submit();
});



// Initialize form validation rules using jQuery Validation plugin
$("#signupForm").validate({

focusInvalid: false,
onkeyup: false,
submitHandler: function (form) {

var formData = $(form).serializeArray();
var userData = {};

formData.forEach(function (data) {
userData[data.name] = data.value;
});

if (debug) alert(JSON.stringify(userData));

localStorage.setItem("userData", JSON.stringify(userData));

$.post("http://localhost:3000/registerUser", userData)
.done(function (data, status, xhr) {
if (debug) alert("Data sent: " + JSON.stringify(data));
if (debug) alert("\nStatus: " + status);
alert("Signing up successfully!");
$("#signupForm").trigger('reset');

$.mobile.changePage("#loginPage");
})
.fail(function (xhr, status, error) {
if (xhr.status === 409) {
alert("User already exists. Please use a different email.");
} else {
alert("An error occurred: " + error);
}
$("#signupForm").trigger('reset');
});
},

rules: {
email: {
required: true,
Expand Down Expand Up @@ -226,9 +221,6 @@ $(document).ready(function () {
});
});




function openNav() {
document.getElementById("mySidenav").style.width = "250px";
}
Expand Down
30 changes: 0 additions & 30 deletions server/apiServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,6 @@ app.get('/', (req, res) => {
res.send('Hello World!')
})

app.get('/getUserDataTest', (req, res) => {

userCollection.find({}, {projection:{_id:0}}).toArray( function(err,docs) {
if(err) {
console.log("Some error.. " + err + "\n");
} else {
console.log( JSON.stringify(docs) + " have been retrieved.\n");
var str = "<h1>" + JSON.stringify(docs) + "</h1>"
str+= "<h1> Error: " + err + "</h1>"
res.send(str);
}
});

});

app.post('/verifyUser', (req, res) => {

loginData = req.body;
Expand Down Expand Up @@ -102,21 +87,6 @@ app.post('/registerUser', (req, res) => {
});
});

app.get('/getOrderDataTest', (req, res) => {

cardCollection.find({},{projection:{_id:0}}).toArray( function(err,docs) {
if(err) {
console.log("Some error.. " + err + "\n");
} else {
console.log( JSON.stringify(docs) + " have been retrieved.\n");
var str = "<h1>" + JSON.stringify(docs) + "</h1>"
str+= "<h1> Error: " + err + "</h1>"
res.send(str);
}
});

});

app.get('/getOrderData', (req, res) => {
console.log("GET request received : " + JSON.stringify(req.body));

Expand Down

0 comments on commit a181f08

Please sign in to comment.