Skip to content

Commit

Permalink
Fixed syntax errors of login.js (#1314)
Browse files Browse the repository at this point in the history
Co-authored-by: Medha <[email protected]>
  • Loading branch information
medss19 and Medha authored Dec 26, 2024
1 parent f32d5e5 commit 219f453
Showing 1 changed file with 111 additions and 194 deletions.
305 changes: 111 additions & 194 deletions login.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -31,244 +30,175 @@ 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 () {
window.location.assign(
`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}`
);
};

// 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");

Expand All @@ -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");
Expand All @@ -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);
});

0 comments on commit 219f453

Please sign in to comment.