Skip to content

Commit

Permalink
Update userID.js
Browse files Browse the repository at this point in the history
  • Loading branch information
woshiJay committed Apr 9, 2024
1 parent 266f3a3 commit 78b0a0f
Showing 1 changed file with 39 additions and 39 deletions.
78 changes: 39 additions & 39 deletions src/auth/userID.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,51 @@
// window.onload = () => {
// const userId = sessionStorage.getItem('userId');
// console.log("this is the userID: ", {userId});
// if (!userId) {
// window.location.href = '../pages/login.html';
// } else {
// fetch(`/get-username?uid=${userId}`)
// .then(resp => resp.json())
// .then(data => {
// // Assuming 'data' has a 'username' property with the user's name
// const username = data.username;
// const usernameElement = document.querySelector('.user-disp');
// // Replace the placeholder text with the actual username
// usernameElement.textContent = `Welcome Back, ${username}.`;
// })
// .catch(error => {
// console.error("Error:", error);
// });
// }
// }

window.onload = () => {
const userId = sessionStorage.getItem('userId');
console.log("this is the userID: ", userId);
console.log("this is the userID: ", {userId});
if (!userId) {
window.location.href = '/pages/login.html'; // This path might need to be adjusted depending on your structure
window.location.href = '../pages/login.html';
} else {
fetch(`/get-username?uid=${userId}`)
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
return response.text(); // Temporarily get raw text to see what's actually coming back
})
.then(text => {
console.log("Received text: ", text); // Log out the raw text response
return JSON.parse(text); // Parse text to JSON
})
.then(resp => resp.json())
.then(data => {
// Assuming 'data' has a 'username' property with the user's name
const username = data.username;
const usernameElement = document.querySelector('.user-disp');
if(data && data.username) {
usernameElement.textContent = `Welcome Back, ${data.username}`;
} else {
throw new Error('Username not found in response');
}
// Replace the placeholder text with the actual username
usernameElement.textContent = `Welcome Back, ${username}.`;
})
.catch(error => {
console.error("Error:", error);
});
}
}
}

// window.onload = () => {
// const userId = sessionStorage.getItem('userId');
// console.log("this is the userID: ", userId);
// if (!userId) {
// window.location.href = '/pages/login.html'; // This path might need to be adjusted depending on your structure
// } else {
// fetch(`/get-username?uid=${userId}`)
// .then(response => {
// if (!response.ok) {
// throw new Error(`HTTP error! Status: ${response.status}`);
// }
// return response.text(); // Temporarily get raw text to see what's actually coming back
// })
// .then(text => {
// console.log("Received text: ", text); // Log out the raw text response
// return JSON.parse(text); // Parse text to JSON
// })
// .then(data => {
// const usernameElement = document.querySelector('.user-disp');
// if(data && data.username) {
// usernameElement.textContent = `Welcome Back, ${data.username}`;
// } else {
// throw new Error('Username not found in response');
// }
// })
// .catch(error => {
// console.error("Error:", error);
// });
// }
// }

0 comments on commit 78b0a0f

Please sign in to comment.