Fix changing password in frontend
This commit is contained in:
parent
2e5334001f
commit
d25b72c3db
@ -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>
|
||||
|
@ -95,26 +95,23 @@ document
|
||||
.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;
|
||||
|
||||
if (newPassword !== repeatPassword) {
|
||||
let errorDiv = document.getElementById("form-error");
|
||||
errorDiv.style.display = "block";
|
||||
errorDiv.innerText = "Passwords do not match!";
|
||||
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";
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -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,
|
||||
});
|
||||
|
@ -42,11 +42,12 @@ 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]
|
||||
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",
|
||||
@ -56,7 +57,7 @@ export function checkTokens() {
|
||||
})
|
||||
.then(async response => {
|
||||
if (!response.ok) {
|
||||
window.location.href = "/login";
|
||||
location.href = "/login";
|
||||
return;
|
||||
}
|
||||
|
||||
@ -68,9 +69,8 @@ export function checkTokens() {
|
||||
return json.token;
|
||||
});
|
||||
}
|
||||
else{
|
||||
window.location.href = "/login";
|
||||
}
|
||||
|
||||
location.href = "/login";
|
||||
}
|
||||
|
||||
export function logout() {
|
||||
|
Loading…
Reference in New Issue
Block a user