From c0b4bea70000ad91e61bbb4014c894060f5d9b71 Mon Sep 17 00:00:00 2001 From: JishnuS420 Date: Thu, 2 Nov 2023 16:39:19 -0700 Subject: [PATCH 1/9] work? --- pages/signin.html | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/pages/signin.html b/pages/signin.html index 8207faf..6f22007 100644 --- a/pages/signin.html +++ b/pages/signin.html @@ -123,8 +123,8 @@

Sign In

} if (response.ok) { console.log('Successful fetch'); + alert("Signed In!"); return; - window.location.href = "https://csa-tri-1.github.io/DADDiJkstra-frontend/pages/app"; } // Success!!! // Redirect to Database location @@ -148,23 +148,19 @@

Sign In

fetch(read_url, options) .then(response => { if (response.status === 200) { - console.log(response); - alert("Signed In!"); - return; window.location.href = "https://csa-tri-1.github.io/DADDiJkstra-frontend/pages/app"; - } - if (response.status !== 200) { - const errorMsg = 'Database response error: ' + response.status; - console.log(errorMsg); - alert("Bad password/username input"); return; - window.location.href = "https://csa-tri-1.github.io/DADDiJkstra-frontend/pages/app"; } if (response.status === 401) { alert("Bad password/username input"); console.log(errorMsg); return; - window.location.href = "https://csa-tri-1.github.io/DADDiJkstra-frontend/pages/app"; + } + if (response.status !== 200) { + const errorMsg = 'Database response error: ' + response.status; + alert("Uh oh something went wrong :(") + console.log(errorMsg); + return; } // response.json().then(data => { // // const jwtToken = data.jwt; From 49f12948ae434cbf48b71498bbc345c6ca04c597 Mon Sep 17 00:00:00 2001 From: JishnuS420 Date: Thu, 2 Nov 2023 16:53:54 -0700 Subject: [PATCH 2/9] commit --- pages/signin.html | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pages/signin.html b/pages/signin.html index 6f22007..0d64e99 100644 --- a/pages/signin.html +++ b/pages/signin.html @@ -151,6 +151,8 @@

Sign In

window.location.href = "https://csa-tri-1.github.io/DADDiJkstra-frontend/pages/app"; return; } + }) + .catch(error =>{ if (response.status === 401) { alert("Bad password/username input"); console.log(errorMsg); @@ -162,6 +164,7 @@

Sign In

console.log(errorMsg); return; } + }); // response.json().then(data => { // // const jwtToken = data.jwt; // // const email = emailInput.value; @@ -171,8 +174,7 @@

Sign In

// alert("Signed In!") // return; // }); - }) - }; + } From b5c0c062df617335fda66c6255f47bd363319bd0 Mon Sep 17 00:00:00 2001 From: JishnuS420 Date: Thu, 2 Nov 2023 17:00:50 -0700 Subject: [PATCH 3/9] fixing errors --- pages/signin.html | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/pages/signin.html b/pages/signin.html index 0d64e99..0a9faa7 100644 --- a/pages/signin.html +++ b/pages/signin.html @@ -123,7 +123,6 @@

Sign In

} if (response.ok) { console.log('Successful fetch'); - alert("Signed In!"); return; } // Success!!! @@ -148,22 +147,20 @@

Sign In

fetch(read_url, options) .then(response => { if (response.status === 200) { + alert("Signed In!"); window.location.href = "https://csa-tri-1.github.io/DADDiJkstra-frontend/pages/app"; - return; - } - }) - .catch(error =>{ - if (response.status === 401) { + } else if (response.status === 401) { alert("Bad password/username input"); - console.log(errorMsg); - return; - } - if (response.status !== 200) { + console.error("Bad password/username input"); // Use console.error for errors + } else { const errorMsg = 'Database response error: ' + response.status; - alert("Uh oh something went wrong :(") - console.log(errorMsg); - return; + alert("Uh oh, something went wrong :("); + console.error(errorMsg); } + }) + .catch(error => { + // Handle network errors or exceptions here + console.error("Network error:", error); }); // response.json().then(data => { // // const jwtToken = data.jwt; From aff1c88849ec9f7e55a6af30bcf75475b38e8238 Mon Sep 17 00:00:00 2001 From: JishnuS420 Date: Thu, 2 Nov 2023 17:18:53 -0700 Subject: [PATCH 4/9] cookies implementation attempt #1 --- pages/signin.html | 42 +++++++++++++----------------------------- 1 file changed, 13 insertions(+), 29 deletions(-) diff --git a/pages/signin.html b/pages/signin.html index 0a9faa7..f058c1a 100644 --- a/pages/signin.html +++ b/pages/signin.html @@ -41,24 +41,6 @@

Sign In

const put_url = "http://localhost:8084/api/usr/update"; let currentField = "emailField"; - // // - // //set a cookie email - // function setCookie(name, email, hours) { - // const expires = new Date(); - // expires.setTime(expires.getTime() + (hours * 60 * 60 * 1000)); - // document.cookie = `${name}=${email};expires=${expires.toUTCString()};path=/`; - // } - - // // const email = emailInput.value; - // // setCookie("email", email, 1); // Expires in 1 hour - - // // Function to get a cookie email - // function getCookie(name) { - // const value = `; ${document.cookie}`; - // const parts = value.split(`; ${name}=`); - // if (parts.length === 2) return parts.pop().split(';').shift(); - // } - // // function showNextField() { const emailField = document.getElementById("emailField"); const passwordField = document.getElementById("passwordField"); @@ -122,6 +104,8 @@

Sign In

return; } if (response.ok) { + // Successfully logged in, now set the email in a cookie + setCookie("email", emailInput.value, 1); // Set the email cookie for an hour console.log('Successful fetch'); return; } @@ -130,7 +114,16 @@

Sign In

}); } } - // new code end + + // Function to set a cookie + function setCookie(name, value, days) { + const date = new Date(); + date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000)); + const expires = "expires=" + date.toUTCString(); + document.cookie = name + "=" + value + ";" + expires + ";path=/"; + } + + // read function read_pull() { // Set options for cross-origin header request const options = { @@ -162,16 +155,7 @@

Sign In

// Handle network errors or exceptions here console.error("Network error:", error); }); - // response.json().then(data => { - // // const jwtToken = data.jwt; - // // const email = emailInput.value; - // // setCookie("email", email, 1); // Expires in 1 hour - // // setCookie("jwt", jwtToken, 1); // Expires in 1 hour - // console.log(response); - // alert("Signed In!") - // return; - // }); - } + } From 0f7f21c0bdcdd015c86632596313e5c3f26e2787 Mon Sep 17 00:00:00 2001 From: JishnuS420 Date: Thu, 2 Nov 2023 17:20:36 -0700 Subject: [PATCH 5/9] cookies implmentation #1 --- pages/signin.html | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/pages/signin.html b/pages/signin.html index f058c1a..5d7e84d 100644 --- a/pages/signin.html +++ b/pages/signin.html @@ -140,11 +140,12 @@

Sign In

fetch(read_url, options) .then(response => { if (response.status === 200) { - alert("Signed In!"); + console.log("Success loser!"); + return; window.location.href = "https://csa-tri-1.github.io/DADDiJkstra-frontend/pages/app"; } else if (response.status === 401) { alert("Bad password/username input"); - console.error("Bad password/username input"); // Use console.error for errors + console.error("Bad password/username input"); } else { const errorMsg = 'Database response error: ' + response.status; alert("Uh oh, something went wrong :("); @@ -155,7 +156,7 @@

Sign In

// Handle network errors or exceptions here console.error("Network error:", error); }); - } + } From ae928f3ed8f7368508444c955c635b35628063b2 Mon Sep 17 00:00:00 2001 From: JishnuS420 Date: Thu, 2 Nov 2023 17:22:39 -0700 Subject: [PATCH 6/9] cookies get function --- pages/signin.html | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/pages/signin.html b/pages/signin.html index 5d7e84d..b988a80 100644 --- a/pages/signin.html +++ b/pages/signin.html @@ -157,6 +157,29 @@

Sign In

console.error("Network error:", error); }); } + + function getEmailFromCookie() { + const name = "email="; + const decodedCookie = decodeURIComponent(document.cookie); + const cookieArray = decodedCookie.split(';'); + for (let i = 0; i < cookieArray.length; i++) { + let cookie = cookieArray[i]; + while (cookie.charAt(0) === ' ') { + cookie = cookie.substring(1); + } + if (cookie.indexOf(name) === 0) { + return cookie.substring(name.length, cookie.length); + } + } + return ""; + } + // Example of how to use the getEmailFromCookie function + const storedEmail = getEmailFromCookie(); + if (storedEmail) { + console.log("Email from cookie:", storedEmail); + } else { + console.log("Email cookie not found"); + } From a779d45a837c8d620afab33369985416b9ac41d2 Mon Sep 17 00:00:00 2001 From: JishnuS420 Date: Thu, 2 Nov 2023 17:25:48 -0700 Subject: [PATCH 7/9] temp removing redirect --- pages/signin.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pages/signin.html b/pages/signin.html index b988a80..faa75df 100644 --- a/pages/signin.html +++ b/pages/signin.html @@ -142,7 +142,7 @@

Sign In

if (response.status === 200) { console.log("Success loser!"); return; - window.location.href = "https://csa-tri-1.github.io/DADDiJkstra-frontend/pages/app"; + // window.location.href = "https://csa-tri-1.github.io/DADDiJkstra-frontend/pages/app"; } else if (response.status === 401) { alert("Bad password/username input"); console.error("Bad password/username input"); From e573e36e88350fdf010e54552058724f48bd063b Mon Sep 17 00:00:00 2001 From: JishnuS420 Date: Thu, 2 Nov 2023 17:44:27 -0700 Subject: [PATCH 8/9] fixing sign in --- pages/signin.html | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/pages/signin.html b/pages/signin.html index faa75df..01d306f 100644 --- a/pages/signin.html +++ b/pages/signin.html @@ -94,7 +94,6 @@

Sign In

"Content-Type": "application/json", }, }; - // new code begin // Fetch JWT fetch(login_url, authOptions) .then(response => { @@ -139,23 +138,28 @@

Sign In

// Fetch the API fetch(read_url, options) .then(response => { - if (response.status === 200) { - console.log("Success loser!"); - return; - // window.location.href = "https://csa-tri-1.github.io/DADDiJkstra-frontend/pages/app"; - } else if (response.status === 401) { + if (response.status === 401) { alert("Bad password/username input"); console.error("Bad password/username input"); + return; + } else if (response.status === 200) { + console.log("Success loser!"); + // Only show the alert if the login was successful + alert("Successful Signin!"); + // Redirect after alert + window.location.href = "https://csa-tri-1.github.io/DADDiJkstra-frontend/pages/app"; + return; } else { const errorMsg = 'Database response error: ' + response.status; alert("Uh oh, something went wrong :("); console.error(errorMsg); + return; } - }) - .catch(error => { - // Handle network errors or exceptions here - console.error("Network error:", error); - }); + }) + .catch(error => { + // Handle network errors or exceptions here + console.error("Network error:", error); + }); } function getEmailFromCookie() { From 947ac482d602b7078bf96b5d5f126464bb13ad21 Mon Sep 17 00:00:00 2001 From: JishnuS420 Date: Thu, 2 Nov 2023 17:49:29 -0700 Subject: [PATCH 9/9] fixing redundancy --- pages/signin.html | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pages/signin.html b/pages/signin.html index 01d306f..e384416 100644 --- a/pages/signin.html +++ b/pages/signin.html @@ -51,14 +51,12 @@

Sign In

document.querySelector("button").textContent = "Log In"; } else if (currentField === "passwordField") { login_user(); - read_pull(); // } const signinForm = document.getElementById("signinForm"); signinForm.addEventListener("submit", function (e) { login_user(e); - read_pull(); // }) } @@ -106,6 +104,7 @@

Sign In

// Successfully logged in, now set the email in a cookie setCookie("email", emailInput.value, 1); // Set the email cookie for an hour console.log('Successful fetch'); + read_pull(); return; } // Success!!!