Fix changing password in frontend

This commit is contained in:
Reimar 2025-04-09 09:43:52 +02:00
parent 2e5334001f
commit d25b72c3db
Signed by: Reimar
GPG Key ID: 93549FA07F0AE268
4 changed files with 81 additions and 83 deletions

View File

@ -4,6 +4,7 @@
<meta charset="UTF-8" />
<title>Register - Temperature Alarm</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="/styles/common.css">
<link rel="stylesheet" href="/styles/auth.css">
<link rel="stylesheet" href="/styles/profile.css">
<script defer type="module" src="/scripts/profile.js"></script>
@ -67,7 +68,7 @@
<button type="submit">Change Password</button>
<div id="form-error"></div>
<div id="password-error" class="error"></div>
</div>
</form>
</div>

View File

@ -8,10 +8,10 @@ var username;
var email;
get().then((res) => {
username = res.userName;
email = res.email;
var table = document.getElementById(`profileCard`);
table.innerHTML += `
username = res.userName;
email = res.email;
var table = document.getElementById(`profileCard`);
table.innerHTML += `
<div class="pfp">
<img style="width:200px; height:200px" src="${profileData.pfp}">
</div>
@ -43,79 +43,76 @@ usernameInput.addEventListener("input", checkForChanges);
// Open modals
editIconbtn.onclick = () => {
document.getElementById("uname").value = username;
document.getElementById("email").value = email;
submitBtn.disabled = true;
editModal.style.display = "block";
document.getElementById("uname").value = username;
document.getElementById("email").value = email;
submitBtn.disabled = true;
editModal.style.display = "block";
};
passwordBtn.onclick = () => (pswModal.style.display = "block");
// Close modals when clicking on any close button
document.querySelectorAll(".close").forEach((closeBtn) => {
closeBtn.onclick = () => {
pswModal.style.display = "none";
editModal.style.display = "none";
document.getElementById("form-error").innerText = "";
document.getElementById("form-error").style.display = "none";
};
closeBtn.onclick = () => {
pswModal.style.display = "none";
editModal.style.display = "none";
document.getElementById("form-error").innerText = "";
document.getElementById("form-error").style.display = "none";
};
});
// Close modals when clicking outside
window.onclick = (event) => {
if (event.target == pswModal || event.target == editModal) {
pswModal.style.display = "none";
editModal.style.display = "none";
document.getElementById("form-error").innerText = "";
document.getElementById("form-error").style.display = "none";
}
if (event.target == pswModal || event.target == editModal) {
pswModal.style.display = "none";
editModal.style.display = "none";
document.getElementById("form-error").innerText = "";
document.getElementById("form-error").style.display = "none";
}
};
document
.getElementById("editForm")
.addEventListener("submit", function (event) {
event.preventDefault(); // Prevents default form submission
.getElementById("editForm")
.addEventListener("submit", function (event) {
event.preventDefault(); // Prevents default form submission
document.getElementById("form-error").style.display = "none";
document.getElementById("form-error").style.display = "none";
// Call function with form values
update(emailInput.value, usernameInput.value).then((response) => {
if (response?.error) {
document.getElementById("form-error").innerText = response.error;
document.getElementById("form-error").style.display = "block";
// Call function with form values
update(emailInput.value, usernameInput.value).then((response) => {
if (response?.error) {
document.getElementById("form-error").innerText = response.error;
document.getElementById("form-error").style.display = "block";
return;
}
return;
}
location.href = "/profile";
location.href = "/profile";
});
});
});
document
.getElementById("PasswordForm")
.addEventListener("submit", function (event) {
event.preventDefault(); // Prevents default form submission
.getElementById("PasswordForm")
.addEventListener("submit", function (event) {
event.preventDefault(); // Prevents default form submission
document.getElementById("form-error").style.display = "none";
document.getElementById("password-error").style.display = "none";
const oldPassword = document.getElementById("oldpsw").value;
const newPassword = document.getElementById("psw").value;
const repeatPassword = document.getElementById("rpsw").value;
const oldPassword = document.getElementById("oldpsw").value;
const newPassword = document.getElementById("psw").value;
const repeatPassword = document.getElementById("rpsw").value;
if (newPassword !== repeatPassword) {
let errorDiv = document.getElementById("form-error");
errorDiv.style.display = "block";
errorDiv.innerText = "Passwords do not match!";
return;
}
if (newPassword !== repeatPassword) {
document.getElementById("password-error").style.display = "block";
document.getElementById("password-error").innerText = "Passwords do not match!";
return;
}
updatePassword(oldPassword, newPassword).then((response) => {
//error messages do not work
if (response.error) {
document.getElementById("form-error").innerText = response.message;
document.getElementById("form-error").style.display = "block";
return;
}
updatePassword(oldPassword, newPassword)
.then(() => location.reload())
.catch(error => {
document.getElementById("password-error").innerText = error;
document.getElementById("password-error").style.display = "block";
});
});
});
document.querySelector(".logout-container").addEventListener("click", logout);

View File

@ -39,7 +39,7 @@ export function update(email, username){
}
export function updatePassword(oldPassword, newPassword){
return request("PUT", "/user/update-password", {
return request("PUT", "/user/change-password", {
oldPassword,
newPassword,
});

View File

@ -41,36 +41,36 @@ export async function request(method, path, body = null) {
export function checkTokens() {
var token = document.cookie.match(/\bauth-token=([^;\s]+)/);
if(token != null){
return token[1]
if (token != null) {
return token[1];
}
const match = document.cookie.match(/\brefresh-token=([^;\s]+)/);
token = match ? match[1] : null;
console.log("refresh "+token);
if(token != null){
return fetch(`${address}/user/refreshtoken/${token}`, {
method: "POST",
headers: {
"Content-Type": "application/json"
},
})
.then(async response => {
if (!response.ok) {
window.location.href = "/login";
return;
}
const json = await response.json()
document.cookie = `auth-token=${json.token}; Path=/`;
document.cookie = `refresh-token=${json.refreshToken}; Path=/`;
return json.token;
});
}
else{
window.location.href = "/login";
}
if (token != null) {
return fetch(`${address}/user/refreshtoken/${token}`, {
method: "POST",
headers: {
"Content-Type": "application/json"
},
})
.then(async response => {
if (!response.ok) {
location.href = "/login";
return;
}
const json = await response.json()
document.cookie = `auth-token=${json.token}; Path=/`;
document.cookie = `refresh-token=${json.refreshToken}; Path=/`;
return json.token;
});
}
location.href = "/login";
}
export function logout() {