From 7641f45abf8de4f93510393e20850f29ba186f26 Mon Sep 17 00:00:00 2001 From: Aravind142857 Date: Sun, 25 Jun 2023 15:10:39 -0500 Subject: [PATCH] Front and Back ends of login and sign up completed --- public/index.html | 2 + public/login.html | 3 + public/signup.html | 7 +- public/src/login.js | 204 +++++++++++++++++++++++++------------------- result.js | 64 ++++++++++++-- 5 files changed, 179 insertions(+), 101 deletions(-) diff --git a/public/index.html b/public/index.html index 3cf1d74..4fd0ed2 100644 --- a/public/index.html +++ b/public/index.html @@ -10,6 +10,7 @@

Link Shortener

Hello There!

+ +
diff --git a/public/login.html b/public/login.html index c458e5e..c3d248a 100644 --- a/public/login.html +++ b/public/login.html @@ -69,6 +69,9 @@ +
diff --git a/public/signup.html b/public/signup.html index 78dc8ff..5b4f232 100644 --- a/public/signup.html +++ b/public/signup.html @@ -12,7 +12,7 @@
- +
@@ -69,10 +69,10 @@
- +
- + Passwords do not match

+
diff --git a/public/src/login.js b/public/src/login.js index b387013..5543c51 100644 --- a/public/src/login.js +++ b/public/src/login.js @@ -2,9 +2,8 @@ let email = document.getElementById("email"); let password = document.getElementById("password"); let pswd_visible = document.getElementById("pswd-visible"); let pswd_invisible = document.getElementById("pswd-invisible"); -let check_pswd_visible = document.getElementById("check-pswd-visible"); -let check_pswd_invisible = document.getElementById("check-pswd-invisible"); -let check_password = document.getElementById("check-password"); + +console.log(window.location.pathname); pswd_visible.addEventListener('click', () => { pswd_visible.classList.add("hidden"); pswd_invisible.classList.remove("hidden"); @@ -15,16 +14,21 @@ pswd_invisible.addEventListener('click', () => { pswd_visible.classList.remove("hidden"); password.type = "text"; }); -check_pswd_visible.addEventListener('click', () => { - check_pswd_visible.classList.add("hidden"); - check_pswd_invisible.classList.remove("hidden"); - check_password.type = "password"; -}); -check_pswd_invisible.addEventListener('click', () => { - check_pswd_invisible.classList.add("hidden"); - check_pswd_visible.classList.remove("hidden"); - check_password.type = "text"; -}); +if (window.location.pathname === '/signup.html') { + let check_pswd_visible = document.getElementById("check-pswd-visible"); + let check_pswd_invisible = document.getElementById("check-pswd-invisible"); + let check_password = document.getElementById("verify"); + check_pswd_visible.addEventListener('click', () => { + check_pswd_visible.classList.add("hidden"); + check_pswd_invisible.classList.remove("hidden"); + check_password.type = "password"; + }); + check_pswd_invisible.addEventListener('click', () => { + check_pswd_invisible.classList.add("hidden"); + check_pswd_visible.classList.remove("hidden"); + check_password.type = "text"; + }); +} let emailRX = new RegExp("^[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+(\\.[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z]+)+$"); let domainError = document.getElementById("domain"); let domainRX = new RegExp("^[a-zA-Z0-9!@#$%^&*]*$"); @@ -39,9 +43,20 @@ let symbolRX = new RegExp(".*[!@#$%^&*]+"); let symbolError = document.getElementById("symbol"); let MIN_LENGTH = 10; let lenError = document.getElementById("length"); -let check_password_error = document.getElementById("check-password-error"); - - +if (window.location.pathname === '/login.html') { + let auth_error = document.getElementById("auth"); + let query = window.location.search; + const urlParams = new URLSearchParams(query); + const errorType = urlParams.get('error'); + console.log(window.location.pathname); + if (errorType === 'auth') { + auth_error.classList.add('block'); + auth_error.classList.remove('hidden'); + } else { + auth_error.classList.remove('block'); + auth_error.classList.add('hidden'); + } +} email.addEventListener('keyup', function(event) { if (emailRX.test(email.value)) { console.log("email valid"); @@ -64,79 +79,90 @@ email.addEventListener('keyup', function(event) { } }); password.addEventListener('keyup', () => { - let pswd = password.value; - if (!capsRX.test(pswd)) { - capsError.classList.remove("hidden"); - capsError.classList.add("block"); - } else { - capsError.classList.add("hidden"); - capsError.classList.remove("block"); - } - if (!lowRX.test(pswd)) { - lowError.classList.remove("hidden"); - lowError.classList.add("block"); + if (document.location.pathname === '/login.html') { + if (document.getElementById('auth').classList.contains('block')) { + document.getElementById('auth').classList.remove('block'); + document.getElementById('auth').classList.add('hidden'); + } } else { - lowError.classList.add("hidden"); - lowError.classList.remove("block"); - } - if (!numRX.test(pswd)) { - numError.classList.remove("hidden"); - numError.classList.add("block"); - } else { - numError.classList.add("hidden"); - numError.classList.remove("block"); - } - if (!symbolRX.test(pswd)) { - symbolError.classList.remove("hidden"); - symbolError.classList.add("block"); - } else { - symbolError.classList.add("hidden"); - symbolError.classList.remove("block"); - } - if (!domainRX.test(pswd)) { - domainError.classList.remove("hidden"); - domainError.classList.add("block"); - } else { - domainError.classList.add("hidden"); - domainError.classList.remove("block"); - } - if (pswd.length < MIN_LENGTH) { - lenError.classList.remove("hidden"); - lenError.classList.add("block"); - } else { - lenError.classList.add("hidden"); - lenError.classList.remove("block"); - } - if (capsError.classList.contains("block") || lowError.classList.contains("block") || numError.classList.contains("block") || symbolError.classList.contains("block") || lenError.classList.contains("block")) { - password.classList.remove("ring-green-400"); - password.classList.remove("focus:ring-green-400"); - password.classList.add("ring-red-400"); - password.classList.add("focus:ring-red-400"); - } else { - password.classList.add("ring-green-400"); - password.classList.add("focus:ring-green-400"); - password.classList.remove("ring-red-400"); - password.classList.remove("focus:ring-red-400"); + let pswd = password.value; + if (!capsRX.test(pswd)) { + capsError.classList.remove("hidden"); + capsError.classList.add("block"); + } else { + capsError.classList.add("hidden"); + capsError.classList.remove("block"); + } + if (!lowRX.test(pswd)) { + lowError.classList.remove("hidden"); + lowError.classList.add("block"); + } else { + lowError.classList.add("hidden"); + lowError.classList.remove("block"); + } + if (!numRX.test(pswd)) { + numError.classList.remove("hidden"); + numError.classList.add("block"); + } else { + numError.classList.add("hidden"); + numError.classList.remove("block"); + } + if (!symbolRX.test(pswd)) { + symbolError.classList.remove("hidden"); + symbolError.classList.add("block"); + } else { + symbolError.classList.add("hidden"); + symbolError.classList.remove("block"); + } + if (!domainRX.test(pswd)) { + domainError.classList.remove("hidden"); + domainError.classList.add("block"); + } else { + domainError.classList.add("hidden"); + domainError.classList.remove("block"); + } + if (pswd.length < MIN_LENGTH) { + lenError.classList.remove("hidden"); + lenError.classList.add("block"); + } else { + lenError.classList.add("hidden"); + lenError.classList.remove("block"); + } + if (capsError.classList.contains("block") || lowError.classList.contains("block") || numError.classList.contains("block") || symbolError.classList.contains("block") || lenError.classList.contains("block")) { + password.classList.remove("ring-green-400"); + password.classList.remove("focus:ring-green-400"); + password.classList.add("ring-red-400"); + password.classList.add("focus:ring-red-400"); + } else { + password.classList.add("ring-green-400"); + password.classList.add("focus:ring-green-400"); + password.classList.remove("ring-red-400"); + password.classList.remove("focus:ring-red-400"); + } } }); -check_password.addEventListener("keyup", () => { - let check = check_password.value; - if (check !== password.value) { - check_password_error.classList.remove("hidden"); - check_password_error.classList.add("block"); - } else { - check_password_error.classList.add("hidden"); - check_password_error.classList.remove("block") - } - if (check_password_error.classList.contains("block")) { - check_password.classList.remove("ring-green-400"); - check_password.classList.remove("focus:ring-green-400"); - check_password.classList.add("ring-red-400"); - check_password.classList.add("focus:ring-red-400"); - } else { - check_password.classList.add("ring-green-400"); - check_password.classList.add("focus:ring-green-400"); - check_password.classList.remove("ring-red-400"); - check_password.classList.remove("focus:ring-red-400"); - } -}); \ No newline at end of file +if (window.location.pathname === '/signup.html') { + let check_password = document.getElementById("verify"); + let check_password_error = document.getElementById("check-password-error"); + check_password.addEventListener("keyup", () => { + let check = check_password.value; + if (check !== password.value) { + check_password_error.classList.remove("hidden"); + check_password_error.classList.add("block"); + } else { + check_password_error.classList.add("hidden"); + check_password_error.classList.remove("block") + } + if (check_password_error.classList.contains("block")) { + check_password.classList.remove("ring-green-400"); + check_password.classList.remove("focus:ring-green-400"); + check_password.classList.add("ring-red-400"); + check_password.classList.add("focus:ring-red-400"); + } else { + check_password.classList.add("ring-green-400"); + check_password.classList.add("focus:ring-green-400"); + check_password.classList.remove("ring-red-400"); + check_password.classList.remove("focus:ring-red-400"); + } + }); +} \ No newline at end of file diff --git a/result.js b/result.js index 6b02907..ebeea5a 100644 --- a/result.js +++ b/result.js @@ -14,6 +14,7 @@ const client = new cassandra.Client({ keyspace: 'glink' }); let id = 1; /* Ideally should initialize id to be nextFromDB or write to file and read */ +let idAccount = 1; const port = 63342; // Port that the server listens on */ const RADIUS_OF_EARTH_IN_MILES = 3958.7614580848; @@ -55,11 +56,16 @@ function newString(n) { return str; } -function nextId() { +function nextIdData() { let next = id; id = id + 1; return next; } +function nextIdAccount() { + let next = idAccount; + idAccount = idAccount + 1; + return next; +} function filter(path) { if (path.match(new RegExp("^/[a-zA-Z]+/*$"))) { if (path.charAt(0) === '/') { @@ -127,7 +133,7 @@ app.post('/__add', function(req, res) { if (input_glink === "") { input_glink = getRandomGLink(); - let currID = nextId(); + let currID = nextIdData(); console.log(currID, input_url, input_glink, geoBool, input_radius, input_latitude, input_longitude) client.execute(query, [currID, input_url, input_glink, geoBool, input_radius, input_latitude, input_longitude], {prepare: true}, function(err,result) { if (err) { @@ -153,7 +159,7 @@ app.post('/__add', function(req, res) { console.log(input_glink); client.execute(selectQuery, [input_glink],{} ,function(err, result) { if (result.rows.length === 0) { - let currID = nextId(); + let currID = nextIdData(); console.log("values are: " + currID, input_url, input_glink, geoBool, input_radius, input_latitude, input_longitude); client.execute(query, [currID, input_url, input_glink, geoBool, input_radius, input_latitude, input_longitude], {prepare: true}, function(err,result) { if (err) { @@ -208,11 +214,42 @@ app.post('/__check', function(req, res) { } }) }) +app.post('/__signup', function(req, res, cb) { + console.log("Entered signup"); + let email = req.body.email; + let password = req.body.password; + let confirm_password = req.body.verify; + console.log(confirm_password); + console.log(email); + console.log(password); + if (password === confirm_password) { + bcrypt.hash(password, saltRounds).then(hash => { + console.log(hash); -app.post('/__login', function(req, res) { + let add_qry = "insert into account (id, email, password) values (?, ?, ?)"; + let id = nextIdAccount(); + + console.log(id, email, hash); + client.execute(add_qry, [id, email, hash], {prepare: true}, function(err, result) { + if (err) { + console.log(err.message); + return cb(""); + } else { + console.log("signed up"); + return cb(""); + } + }) + }) + } else { + console.log("Passwords don't match"); + return cb(""); + } +}) +app.post('/__login', function(req, res, cb) { + console.log("entered login"); let email = req.body.email; let password = req.body.password; - /** TODO: Validate to make sure user-password exists */ + /** : Validate to make sure user-password exists */ let emailRX = new RegExp("^[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+(\\.[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z]+)+$"); let domainRX = new RegExp("[A-Za-z0-9!@#$%^&*]"); let minRXCharUp = new RegExp("[A-Z]"); @@ -224,21 +261,30 @@ app.post('/__login', function(req, res) { client.execute(selQry, [email], {}, function(error, result) { if (error) { console.log(error.message); + return cb(""); } else { - if (result.rows[0].length === 0) { - res.redirect("/error.html"); + if (result.rows.length === 0) { + console.log("Wrong email"); + res.redirect("/login.html?error=auth"); + return cb(""); } else { let hash = result.rows[0]["password"]; + console.log(password, hash); bcrypt.compare(password, hash) .then(match => { if (match) { - + console.log("Logged in"); + res.redirect("/index.html"); + return cb(""); } else { - res.redirect("/login.html"); + console.log("Wrong password"); + res.redirect("/login.html?error=auth"); + return cb(""); } }) .catch(err => { console.log(err.message); + return cb(""); }) } }