blink/frontend/vanilla/html/reset-password.html

145 lines
6.3 KiB
HTML

<html>
<head>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"
integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous">
</script>
</head>
<body style="display: none;">
<div class="alert alert-success" role="alert" id="successAlert" style="display: none;">
</div>
<div class="alert alert-danger" role="alert" id="errorAlert" style="display: none;">
</div>
<div class="container d-flex flex-column">
<div class="row align-items-center justify-content-center
min-vh-100 g-0">
<div class="col-12 col-md-8 col-lg-4 border-top border-3 border-primary">
<div class="card shadow-sm">
<div class="card-body">
<div class="mb-4">
<h5>Reset password</h5>
</div>
<form>
<div class="mb-3">
<input type="password" id="password" class="form-control" name="password"
placeholder="Your new password">
<div class="invalid-feedback" id="password-invalid-feedback">
</div>
</div>
<div class="mb-3">
<input type="password" id="confirmPassword" class="form-control" name="confirmPassword"
placeholder="Confirm password">
<div class="invalid-feedback" id="confirmpassword-invalid-feedback">
</div>
</div>
<div class="mb-3 d-grid">
<button type="button" class="btn btn-primary" onclick="resetPassword();">
Reset
</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
<script src="../js/constants.js"></script>
<script src="../js/utils.js"></script>
<script>
window.addEventListener("load", function () {
const secret = new URLSearchParams(window.location.search).get('secret');
if (!secret) {
alert('Invalid URL');
} else {
document.body.style.display = "block"; // Show page
}
});
async function resetPassword() {
if (!validateFields()) {
return;
}
const password = document.getElementById("password").value;
const secret = new URLSearchParams(window.location.search).get('secret');
const response = await fetch(`${API_URL}/resetpassword/reset`, {
method: "POST",
body: JSON.stringify({
password,
secret
}),
headers: createHeaders(null)
});
if (response.ok) {
showSuccessAlert('Your password has been changed. You can now <a href="/login.html">log in</a>');
} else {
showErrorAlert(
'URL either not valid or the link has expired. Please require another <a href="forgot-password.html">password reset</a>'
);
}
}
function validateFields() {
const passwordField = document.getElementById("password");
const confirmPasswordField = document.getElementById("confirmPassword");
var isFormValid = true;
if (!passwordField.value) {
passwordField.classList.add("is-invalid");
document.getElementById('password-invalid-feedback').innerHTML = 'Please fill out this field';
isFormValid = false;
} else if (passwordField.value.length < 5) {
passwordField.classList.add("is-invalid");
document.getElementById('password-invalid-feedback').innerHTML =
'Password must be at least 5 characters';
isFormValid = false;
} else if (passwordField.value != confirmPasswordField.value) {
passwordField.classList.add("is-invalid");
document.getElementById('password-invalid-feedback').innerHTML = 'Passwords do not match';
isFormValid = false;
}
if (!confirmPasswordField.value) {
confirmPasswordField.classList.add("is-invalid");
document.getElementById('confirmpassword-invalid-feedback').innerHTML = 'Please fill out this field';
isFormValid = false;
} else if (confirmPasswordField.value.length < 5) {
confirmPasswordField.classList.add("is-invalid");
document.getElementById('confirmpassword-invalid-feedback').innerHTML =
'Password must be at least 5 characters';
isFormValid = false;
} else if (passwordField.value != confirmPasswordField.value) {
confirmPasswordField.classList.add("is-invalid");
document.getElementById('confirmpassword-invalid-feedback').innerHTML = 'Passwords do not match';
isFormValid = false;
}
if (isFormValid) {
confirmPasswordField.classList.remove("is-invalid");
passwordField.classList.remove("is-invalid");
}
return isFormValid;
}
function showSuccessAlert(message) {
const successAlert = document.getElementById('successAlert');
successAlert.innerHTML = message;
successAlert.style.display = 'block';
}
function showErrorAlert(message) {
const errorAlert = document.getElementById('errorAlert');
errorAlert.innerHTML = message;
errorAlert.style.display = 'block';
}
</script>
</body>
</html>