Skip to content

Commit 219f453

Browse files
medss19Medha
andauthored
Fixed syntax errors of login.js (#1314)
Co-authored-by: Medha <[email protected]>
1 parent f32d5e5 commit 219f453

File tree

1 file changed

+111
-194
lines changed

1 file changed

+111
-194
lines changed

login.js

Lines changed: 111 additions & 194 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ document.addEventListener("DOMContentLoaded", () => {
2121
let GITHUB_CLIENT_ID = "your_github_client_id";
2222
const BACKEND_URL = "http://localhost:3000";
2323

24-
2524
(function () {
2625
const params = window.location.search;
2726
const urlParams = new URLSearchParams(params);
@@ -31,244 +30,175 @@ document.addEventListener("DOMContentLoaded", () => {
3130

3231
function getGhUser() {
3332
let code = localStorage.getItem("code");
34-
fetch(`${BACKEND_URL}/api/auth/github?code=${code}`)
35-
.then((res) => res.json())
36-
.then((response) => {
37-
let resData = response.data;
38-
let token = new URLSearchParams(resData).get("access_token");
33+
fetch(`${BACKEND_URL}/api/auth/github?code=${code}`)
34+
.then((res) => res.json())
35+
.then((response) => {
36+
let resData = response.data;
37+
let token = new URLSearchParams(resData).get("access_token");
3938

40-
fetch(`${BACKEND_URL}/api/auth/github/getUser`, {
41-
headers: {
42-
Authorization: "Bearer " + token,
43-
},
44-
})
45-
.then((res) => res.json())
46-
.then((response) => {
47-
const { name, email } = response.user;
48-
//save the user information into the localStorage for now
49-
localStorage.setItem(
50-
"user-info",
51-
JSON.stringify({
52-
name: name,
53-
email: email,
54-
})
55-
);
56-
//remove the code after saving the user-info & redirect the user to the home page
57-
localStorage.removeItem("code");
58-
window.location.href = "/";
59-
});
60-
});
61-
62-
39+
fetch(`${BACKEND_URL}/api/auth/github/getUser`, {
40+
headers: {
41+
Authorization: "Bearer " + token,
42+
},
43+
})
44+
.then((res) => res.json())
45+
.then((response) => {
46+
const { name, email } = response.user;
47+
//save the user information into the localStorage for now
48+
localStorage.setItem(
49+
"user-info",
50+
JSON.stringify({
51+
name: name,
52+
email: email,
53+
})
54+
);
55+
//remove the code after saving the user-info & redirect the user to the home page
56+
localStorage.removeItem("code");
57+
window.location.href = "/";
58+
});
59+
});
6360
}
6461

65-
6662
//if "user-info" already exists, don't let the user access the login route
67-
if(localStorage.getItem("user-info"))
68-
{
69-
window.location.href =
70-
"/";
71-
}else if(localStorage.getItem("code") && localStorage.getItem("code")!=="null")
72-
{
63+
if(localStorage.getItem("user-info")) {
64+
window.location.href = "/";
65+
} else if(localStorage.getItem("code") && localStorage.getItem("code")!=="null") {
7366
//if the user doesn't exist and code exists in localStorage, get the user information
74-
getGhUser()
75-
76-
// Dummy login logic for demo purposes
77-
if (username === localStorage.getItem('username') && password === localStorage.getItem('password')) {
78-
alert('Login successful!');
79-
// Redirect to stats dashboard page
80-
window.location.href = 'pages/stats.html';
81-
} else {
82-
alert('Invalid username or password');
67+
getGhUser();
8368
}
84-
85-
86-
8769
})();
8870

8971
github_signIn.onclick = function () {
9072
window.location.assign(
9173
`https://github.com/login/oauth/authorize?client_id=${GITHUB_CLIENT_ID}`
9274
);
9375
};
76+
9477
github_login.onclick = function () {
9578
window.location.assign(
9679
`https://github.com/login/oauth/authorize?client_id=${GITHUB_CLIENT_ID}`
9780
);
9881
};
9982

10083
// Sign-in form submission
101-
document
102-
.querySelector(".sign-in-form")
103-
.addEventListener("submit", function (event) {
104-
event.preventDefault();
105-
106-
// Get the input values
107-
const username = document.querySelector(
108-
".sign-in-form input[type='text']"
109-
).value;
110-
const password = document.querySelector(
111-
".sign-in-form input[type='password']"
112-
).value;
113-
114-
// Dummy login logic for demo purposes
115-
if (username === "admin" && password === "password") {
116-
alert("Login successful!");
117-
// Redirect to dashboard page
118-
window.location.href = "index.html";
119-
} else {
120-
alert("Invalid username or password");
121-
}
122-
});
123-
124-
});
125-
document.addEventListener("DOMContentLoaded", function () {
126-
const rememberMeCheckbox = document.getElementById("remember-me");
127-
const usernameInput = document.getElementById("username");
128-
129-
// Load saved username if it exists
130-
if (localStorage.getItem("rememberedUsername")) {
131-
usernameInput.value = localStorage.getItem("rememberedUsername");
132-
rememberMeCheckbox.checked = true;
133-
}
84+
document.querySelector(".sign-in-form").addEventListener("submit", function (event) {
85+
event.preventDefault();
13486

135-
// When the form is submitted, save the username if "Remember Me" is checked
136-
document.querySelector(".sign-in-form").addEventListener("submit", function (e) {
137-
e.preventDefault(); // Prevent default form submission
87+
// Get the input values
88+
const username = document.querySelector(".sign-in-form input[type='text']").value;
89+
const password = document.querySelector(".sign-in-form input[type='password']").value;
13890

139-
if (rememberMeCheckbox.checked) {
140-
localStorage.setItem("rememberedUsername", usernameInput.value);
91+
// Dummy login logic for demo purposes
92+
if (username === "admin" && password === "password") {
93+
alert("Login successful!");
94+
// Redirect to dashboard page
95+
window.location.href = "index.html";
14196
} else {
142-
localStorage.removeItem("rememberedUsername");
97+
alert("Invalid username or password");
14398
}
144-
145-
// Add form submission logic here
14699
});
147-
});
148-
149100

101+
document.addEventListener("DOMContentLoaded", function () {
102+
const rememberMeCheckbox = document.getElementById("remember-me");
103+
const usernameInput = document.getElementById("username");
150104

151-
// Sign-up form submission
152-
document
153-
.querySelector(".sign-up-form")
154-
.addEventListener("submit", function (event) {
155-
event.preventDefault();
156-
157-
// Get the input values
158-
const username = document.querySelector(
159-
".sign-up-form input[type='text']"
160-
).value;
161-
const email = document.querySelector(
162-
".sign-up-form input[type='email']"
163-
).value;
164-
const password = document.querySelector(
165-
".sign-up-form input[type='password']"
166-
).value;
167-
168-
if (username === "" || email === "" || password === "") {
169-
alert("Please fill in all fields");
170-
return;
171-
}
172-
function isValidEmail(email) {
173-
// Regular expression for stricter email validation
174-
const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
105+
// Load saved username if it exists
106+
if (localStorage.getItem("rememberedUsername")) {
107+
usernameInput.value = localStorage.getItem("rememberedUsername");
108+
rememberMeCheckbox.checked = true;
109+
}
175110

111+
// When the form is submitted, save the username if "Remember Me" is checked
112+
document.querySelector(".sign-in-form").addEventListener("submit", function (e) {
113+
e.preventDefault(); // Prevent default form submission
176114

177-
// Check for the basic format
178-
if (!emailRegex.test(email)) {
179-
return false;
180-
}
115+
if (rememberMeCheckbox.checked) {
116+
localStorage.setItem("rememberedUsername", usernameInput.value);
117+
} else {
118+
localStorage.removeItem("rememberedUsername");
119+
}
120+
// Add form submission logic here
121+
});
122+
});
181123

182-
// Split the email into local part and domain part
183-
const [localPart, domainPart] = email.split("@");
124+
// Sign-up form submission
125+
document.querySelector(".sign-up-form").addEventListener("submit", function (event) {
126+
event.preventDefault();
184127

185-
// Ensure local part and domain part exist and aren't too long
186-
if (localPart.length > 64 || domainPart.length > 255) {
187128
// Get the input values
188129
const username = document.querySelector(".sign-up-form input[type='text']").value;
189130
const email = document.querySelector(".sign-up-form input[type='email']").value;
190131
const password = document.querySelector(".sign-up-form input[type='password']").value;
191-
const gitUsername = document.querySelector(".sign-up-form input[type='text'][placeholder='Git Username']").value; // Git Username
192132

193-
194-
if (username === '' || email === '' || password === '' || gitUsername === '') {
195-
alert('Please fill in all fields');
133+
if (username === "" || email === "" || password === "") {
134+
alert("Please fill in all fields");
196135
return;
197136
}
137+
198138
function isValidEmail(email) {
199139
// Regular expression for stricter email validation
200140
const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
201-
141+
202142
// Check for the basic format
203143
if (!emailRegex.test(email)) {
144+
return false;
145+
}
146+
147+
// Split the email into local part and domain part
148+
const [localPart, domainPart] = email.split("@");
204149

205-
return false;
206-
}
207-
208-
// Ensure domain part has a valid format
209-
const domainParts = domainPart.split(".");
210-
if (domainParts.some((part) => part.length > 63)) {
211-
return false;
212-
}
213-
214-
// Additional checks for edge cases
215-
if (
216-
localPart.startsWith(".") ||
217-
localPart.endsWith(".") ||
218-
localPart.includes("..")
219-
) {
220-
return false;
221-
}
222-
223-
return true;
150+
// Ensure local part and domain part exist and aren't too long
151+
if (localPart.length > 64 || domainPart.length > 255) {
152+
return false;
224153
}
225154

226-
// Function to validate username format
227-
function validateUsername(username) {
228-
// Ensure the username is 3-20 characters long, alphanumeric, and contains at least one letter
229-
const usernamePattern = /^(?=.*[a-zA-Z])[a-zA-Z0-9]{3,20}$/;
230-
return usernamePattern.test(username);
155+
// Ensure domain part has a valid format
156+
const domainParts = domainPart.split(".");
157+
if (domainParts.some((part) => part.length > 63)) {
158+
return false;
231159
}
232160

233-
if (!validateUsername(username)) {
234-
alert("Please enter a valid username (3-20 alphanumeric characters).");
235-
return;
161+
// Additional checks for edge cases
162+
if (
163+
localPart.startsWith(".") ||
164+
localPart.endsWith(".") ||
165+
localPart.includes("..")
166+
) {
167+
return false;
236168
}
237169

170+
return true;
171+
}
238172

239-
// Validate email
240-
if (!isValidEmail(email)) {
241-
alert("Please enter a valid email address.");
242-
return;
243-
}
244-
// Dummy signup logic for demo purposes
245-
localStorage.setItem("username", username);
246-
localStorage.setItem("email", email);
247-
localStorage.setItem("password", password);
248-
localStorage.setItem("isLoggedIn", "true");
173+
// Function to validate username format
174+
function validateUsername(username) {
175+
// Ensure the username is 3-20 characters long, alphanumeric, and contains at least one letter
176+
const usernamePattern = /^(?=.*[a-zA-Z])[a-zA-Z0-9]{3,20}$/;
177+
return usernamePattern.test(username);
178+
}
249179

250-
alert("Signup successful!");
251-
// Redirect to dashboard page
252-
window.location.href = "index.html";
253-
});
180+
if (!validateUsername(username)) {
181+
alert("Please enter a valid username (3-20 alphanumeric characters).");
182+
return;
183+
}
254184

255185
// Validate email
256186
if (!isValidEmail(email)) {
257-
alert('Please enter a valid email address.');
187+
alert("Please enter a valid email address.");
258188
return;
259189
}
190+
260191
// Dummy signup logic for demo purposes
261-
localStorage.setItem('username', username);
262-
localStorage.setItem('gitUsername', gitUsername);
263-
localStorage.setItem('email', email);
264-
localStorage.setItem('password', password);
265-
localStorage.setItem('isLoggedIn', 'true');
266-
alert('Signup successful!');
192+
localStorage.setItem("username", username);
193+
localStorage.setItem("email", email);
194+
localStorage.setItem("password", password);
195+
localStorage.setItem("isLoggedIn", "true");
196+
197+
alert("Signup successful!");
267198
// Redirect to dashboard page
268-
window.location.href = 'index.html';
199+
window.location.href = "index.html";
269200
});
270201

271-
272202
// Toggle password visibility for sign-up form
273203
const forms = document.querySelectorAll("form");
274204

@@ -291,9 +221,7 @@ document.addEventListener("DOMContentLoaded", () => {
291221

292222
// Check password strength for sign-up form
293223
function checkPasswordStrength() {
294-
const password = document.querySelector(
295-
".sign-up-form input[type='password']"
296-
).value;
224+
const password = document.querySelector(".sign-up-form input[type='password']").value;
297225
const strengthWeak = document.getElementById("strength-weak");
298226
const strengthMedium = document.getElementById("strength-medium");
299227
const strengthStrong = document.getElementById("strength-strong");
@@ -318,16 +246,5 @@ document.addEventListener("DOMContentLoaded", () => {
318246
}
319247

320248
// Monitor password input on the sign-up form to check password strength
321-
322-
document
323-
.querySelector(".sign-up-form input[type='password']")
324-
.addEventListener("input", checkPasswordStrength);
325-
});
326-
327-
document.querySelector(".sign-up-form input[type='password']").addEventListener('input', checkPasswordStrength);
328-
329-
});
330-
331-
332-
333-
249+
document.querySelector(".sign-up-form input[type='password']").addEventListener("input", checkPasswordStrength);
250+
});

0 commit comments

Comments
 (0)