From 219f4533bbbf9de9ef3d1941e3e2a6e7096c84e1 Mon Sep 17 00:00:00 2001 From: Medha <90996890+medss19@users.noreply.github.com> Date: Thu, 26 Dec 2024 07:37:45 +0530 Subject: [PATCH] Fixed syntax errors of login.js (#1314) Co-authored-by: Medha --- login.js | 305 ++++++++++++++++++++----------------------------------- 1 file changed, 111 insertions(+), 194 deletions(-) diff --git a/login.js b/login.js index ff8e982a..cab95c1e 100644 --- a/login.js +++ b/login.js @@ -21,7 +21,6 @@ document.addEventListener("DOMContentLoaded", () => { let GITHUB_CLIENT_ID = "your_github_client_id"; const BACKEND_URL = "http://localhost:3000"; - (function () { const params = window.location.search; const urlParams = new URLSearchParams(params); @@ -31,59 +30,42 @@ document.addEventListener("DOMContentLoaded", () => { function getGhUser() { let code = localStorage.getItem("code"); - fetch(`${BACKEND_URL}/api/auth/github?code=${code}`) - .then((res) => res.json()) - .then((response) => { - let resData = response.data; - let token = new URLSearchParams(resData).get("access_token"); + fetch(`${BACKEND_URL}/api/auth/github?code=${code}`) + .then((res) => res.json()) + .then((response) => { + let resData = response.data; + let token = new URLSearchParams(resData).get("access_token"); - fetch(`${BACKEND_URL}/api/auth/github/getUser`, { - headers: { - Authorization: "Bearer " + token, - }, - }) - .then((res) => res.json()) - .then((response) => { - const { name, email } = response.user; - //save the user information into the localStorage for now - localStorage.setItem( - "user-info", - JSON.stringify({ - name: name, - email: email, - }) - ); - //remove the code after saving the user-info & redirect the user to the home page - localStorage.removeItem("code"); - window.location.href = "/"; - }); - }); - - + fetch(`${BACKEND_URL}/api/auth/github/getUser`, { + headers: { + Authorization: "Bearer " + token, + }, + }) + .then((res) => res.json()) + .then((response) => { + const { name, email } = response.user; + //save the user information into the localStorage for now + localStorage.setItem( + "user-info", + JSON.stringify({ + name: name, + email: email, + }) + ); + //remove the code after saving the user-info & redirect the user to the home page + localStorage.removeItem("code"); + window.location.href = "/"; + }); + }); } - //if "user-info" already exists, don't let the user access the login route - if(localStorage.getItem("user-info")) - { - window.location.href = - "/"; - }else if(localStorage.getItem("code") && localStorage.getItem("code")!=="null") - { + if(localStorage.getItem("user-info")) { + window.location.href = "/"; + } else if(localStorage.getItem("code") && localStorage.getItem("code")!=="null") { //if the user doesn't exist and code exists in localStorage, get the user information - getGhUser() - - // Dummy login logic for demo purposes - if (username === localStorage.getItem('username') && password === localStorage.getItem('password')) { - alert('Login successful!'); - // Redirect to stats dashboard page - window.location.href = 'pages/stats.html'; - } else { - alert('Invalid username or password'); + getGhUser(); } - - - })(); github_signIn.onclick = function () { @@ -91,6 +73,7 @@ document.addEventListener("DOMContentLoaded", () => { `https://github.com/login/oauth/authorize?client_id=${GITHUB_CLIENT_ID}` ); }; + github_login.onclick = function () { window.location.assign( `https://github.com/login/oauth/authorize?client_id=${GITHUB_CLIENT_ID}` @@ -98,177 +81,124 @@ document.addEventListener("DOMContentLoaded", () => { }; // Sign-in form submission - document - .querySelector(".sign-in-form") - .addEventListener("submit", function (event) { - event.preventDefault(); - - // Get the input values - const username = document.querySelector( - ".sign-in-form input[type='text']" - ).value; - const password = document.querySelector( - ".sign-in-form input[type='password']" - ).value; - - // Dummy login logic for demo purposes - if (username === "admin" && password === "password") { - alert("Login successful!"); - // Redirect to dashboard page - window.location.href = "index.html"; - } else { - alert("Invalid username or password"); - } - }); - - }); - document.addEventListener("DOMContentLoaded", function () { - const rememberMeCheckbox = document.getElementById("remember-me"); - const usernameInput = document.getElementById("username"); - - // Load saved username if it exists - if (localStorage.getItem("rememberedUsername")) { - usernameInput.value = localStorage.getItem("rememberedUsername"); - rememberMeCheckbox.checked = true; - } + document.querySelector(".sign-in-form").addEventListener("submit", function (event) { + event.preventDefault(); - // When the form is submitted, save the username if "Remember Me" is checked - document.querySelector(".sign-in-form").addEventListener("submit", function (e) { - e.preventDefault(); // Prevent default form submission + // Get the input values + const username = document.querySelector(".sign-in-form input[type='text']").value; + const password = document.querySelector(".sign-in-form input[type='password']").value; - if (rememberMeCheckbox.checked) { - localStorage.setItem("rememberedUsername", usernameInput.value); + // Dummy login logic for demo purposes + if (username === "admin" && password === "password") { + alert("Login successful!"); + // Redirect to dashboard page + window.location.href = "index.html"; } else { - localStorage.removeItem("rememberedUsername"); + alert("Invalid username or password"); } - - // Add form submission logic here }); -}); - + document.addEventListener("DOMContentLoaded", function () { + const rememberMeCheckbox = document.getElementById("remember-me"); + const usernameInput = document.getElementById("username"); - // Sign-up form submission - document - .querySelector(".sign-up-form") - .addEventListener("submit", function (event) { - event.preventDefault(); - - // Get the input values - const username = document.querySelector( - ".sign-up-form input[type='text']" - ).value; - const email = document.querySelector( - ".sign-up-form input[type='email']" - ).value; - const password = document.querySelector( - ".sign-up-form input[type='password']" - ).value; - - if (username === "" || email === "" || password === "") { - alert("Please fill in all fields"); - return; - } - function isValidEmail(email) { - // Regular expression for stricter email validation - const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/; + // Load saved username if it exists + if (localStorage.getItem("rememberedUsername")) { + usernameInput.value = localStorage.getItem("rememberedUsername"); + rememberMeCheckbox.checked = true; + } + // When the form is submitted, save the username if "Remember Me" is checked + document.querySelector(".sign-in-form").addEventListener("submit", function (e) { + e.preventDefault(); // Prevent default form submission - // Check for the basic format - if (!emailRegex.test(email)) { - return false; - } + if (rememberMeCheckbox.checked) { + localStorage.setItem("rememberedUsername", usernameInput.value); + } else { + localStorage.removeItem("rememberedUsername"); + } + // Add form submission logic here + }); + }); - // Split the email into local part and domain part - const [localPart, domainPart] = email.split("@"); + // Sign-up form submission + document.querySelector(".sign-up-form").addEventListener("submit", function (event) { + event.preventDefault(); - // Ensure local part and domain part exist and aren't too long - if (localPart.length > 64 || domainPart.length > 255) { // Get the input values const username = document.querySelector(".sign-up-form input[type='text']").value; const email = document.querySelector(".sign-up-form input[type='email']").value; const password = document.querySelector(".sign-up-form input[type='password']").value; - const gitUsername = document.querySelector(".sign-up-form input[type='text'][placeholder='Git Username']").value; // Git Username - - if (username === '' || email === '' || password === '' || gitUsername === '') { - alert('Please fill in all fields'); + if (username === "" || email === "" || password === "") { + alert("Please fill in all fields"); return; } + function isValidEmail(email) { // Regular expression for stricter email validation const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/; - + // Check for the basic format if (!emailRegex.test(email)) { + return false; + } + + // Split the email into local part and domain part + const [localPart, domainPart] = email.split("@"); - return false; - } - - // Ensure domain part has a valid format - const domainParts = domainPart.split("."); - if (domainParts.some((part) => part.length > 63)) { - return false; - } - - // Additional checks for edge cases - if ( - localPart.startsWith(".") || - localPart.endsWith(".") || - localPart.includes("..") - ) { - return false; - } - - return true; + // Ensure local part and domain part exist and aren't too long + if (localPart.length > 64 || domainPart.length > 255) { + return false; } - // Function to validate username format - function validateUsername(username) { - // Ensure the username is 3-20 characters long, alphanumeric, and contains at least one letter - const usernamePattern = /^(?=.*[a-zA-Z])[a-zA-Z0-9]{3,20}$/; - return usernamePattern.test(username); + // Ensure domain part has a valid format + const domainParts = domainPart.split("."); + if (domainParts.some((part) => part.length > 63)) { + return false; } - if (!validateUsername(username)) { - alert("Please enter a valid username (3-20 alphanumeric characters)."); - return; + // Additional checks for edge cases + if ( + localPart.startsWith(".") || + localPart.endsWith(".") || + localPart.includes("..") + ) { + return false; } + return true; + } - // Validate email - if (!isValidEmail(email)) { - alert("Please enter a valid email address."); - return; - } - // Dummy signup logic for demo purposes - localStorage.setItem("username", username); - localStorage.setItem("email", email); - localStorage.setItem("password", password); - localStorage.setItem("isLoggedIn", "true"); + // Function to validate username format + function validateUsername(username) { + // Ensure the username is 3-20 characters long, alphanumeric, and contains at least one letter + const usernamePattern = /^(?=.*[a-zA-Z])[a-zA-Z0-9]{3,20}$/; + return usernamePattern.test(username); + } - alert("Signup successful!"); - // Redirect to dashboard page - window.location.href = "index.html"; - }); + if (!validateUsername(username)) { + alert("Please enter a valid username (3-20 alphanumeric characters)."); + return; + } // Validate email if (!isValidEmail(email)) { - alert('Please enter a valid email address.'); + alert("Please enter a valid email address."); return; } + // Dummy signup logic for demo purposes - localStorage.setItem('username', username); - localStorage.setItem('gitUsername', gitUsername); - localStorage.setItem('email', email); - localStorage.setItem('password', password); - localStorage.setItem('isLoggedIn', 'true'); - alert('Signup successful!'); + localStorage.setItem("username", username); + localStorage.setItem("email", email); + localStorage.setItem("password", password); + localStorage.setItem("isLoggedIn", "true"); + + alert("Signup successful!"); // Redirect to dashboard page - window.location.href = 'index.html'; + window.location.href = "index.html"; }); - // Toggle password visibility for sign-up form const forms = document.querySelectorAll("form"); @@ -291,9 +221,7 @@ document.addEventListener("DOMContentLoaded", () => { // Check password strength for sign-up form function checkPasswordStrength() { - const password = document.querySelector( - ".sign-up-form input[type='password']" - ).value; + const password = document.querySelector(".sign-up-form input[type='password']").value; const strengthWeak = document.getElementById("strength-weak"); const strengthMedium = document.getElementById("strength-medium"); const strengthStrong = document.getElementById("strength-strong"); @@ -318,16 +246,5 @@ document.addEventListener("DOMContentLoaded", () => { } // Monitor password input on the sign-up form to check password strength - - document - .querySelector(".sign-up-form input[type='password']") - .addEventListener("input", checkPasswordStrength); -}); - - document.querySelector(".sign-up-form input[type='password']").addEventListener('input', checkPasswordStrength); - -}); - - - - + document.querySelector(".sign-up-form input[type='password']").addEventListener("input", checkPasswordStrength); +}); \ No newline at end of file