Skip to content

Commit 7885017

Browse files
Block invalid passwords from the frontend form
1 parent 2864a29 commit 7885017

File tree

3 files changed

+36
-13
lines changed

3 files changed

+36
-13
lines changed

main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ async def needs_new_tokens_handler(request: Request, exc: NeedsNewTokens):
5959
return response
6060

6161

62-
# Handle PasswordValidationError by rendering the error page
62+
# Handle PasswordValidationError by rendering the validation_error page
6363
@app.exception_handler(PasswordValidationError)
6464
async def password_validation_exception_handler(request: Request, exc: PasswordValidationError):
6565
return templates.TemplateResponse(
@@ -73,7 +73,7 @@ async def password_validation_exception_handler(request: Request, exc: PasswordV
7373
)
7474

7575

76-
# Handle RequestValidationError by rendering the error page (TODO: use toast instead?)
76+
# Handle RequestValidationError by rendering the validation_error page
7777
@app.exception_handler(RequestValidationError)
7878
async def validation_exception_handler(request: Request, exc: RequestValidationError):
7979
errors = {}

templates/authentication/register.html

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@
3434
<!-- Confirm Password Input -->
3535
<div class="mb-3">
3636
<label for="confirm_password" class="form-label">Confirm Password</label>
37-
<input type="password" class="form-control" id="confirm_password" name="confirm_password" placeholder="Confirm your password" required>
37+
<input type="password" class="form-control" id="confirm_password" name="confirm_password"
38+
placeholder="Confirm your password" required>
39+
<div class="invalid-feedback">
40+
Passwords do not match.
41+
</div>
3842
</div>
3943

4044
<!-- Submit Button -->
@@ -47,13 +51,16 @@
4751
<p class="mt-3 text-center">Already have an account? <a href="{{ url_for('read_login') }}">Login here</a></p>
4852
</div>
4953

50-
<style>
51-
.invalid-feedback {
52-
display: none;
53-
color: red;
54-
}
55-
input:invalid:not(:placeholder-shown) + .invalid-feedback {
56-
display: block;
57-
}
58-
</style>
54+
<script>
55+
// JavaScript to validate password match
56+
document.getElementById('confirm_password').addEventListener('input', function() {
57+
const password = document.getElementById('password').value;
58+
const confirmPassword = this.value;
59+
if (password !== confirmPassword) {
60+
this.setCustomValidity('Passwords do not match.');
61+
} else {
62+
this.setCustomValidity('');
63+
}
64+
});
65+
</script>
5966
{% endblock %}

templates/authentication/reset_password.html

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<div class="mb-3">
1818
<label for="new_password" class="form-label">New Password</label>
1919
<input type="password" class="form-control" id="new_password" name="new_password"
20-
pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}"
20+
pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@$!%*?&\{\}\<\>\.\,\\\\'#\-_=\+\(\)\[\]:;\|~])[A-Za-z\d@$!%*?&\{\}\<\>\.\,\\\\'#\-_=\+\(\)\[\]:;\|~]{8,}"
2121
title="Must contain at least one number, one uppercase and lowercase letter, one special character, and at least 8 or more characters"
2222
required>
2323
<div class="invalid-feedback">
@@ -29,6 +29,9 @@
2929
<div class="mb-3">
3030
<label for="confirm_new_password" class="form-label">Confirm New Password</label>
3131
<input type="password" class="form-control" id="confirm_new_password" name="confirm_new_password" required>
32+
<div class="invalid-feedback">
33+
Passwords do not match.
34+
</div>
3235
</div>
3336

3437
<!-- Submit Button -->
@@ -40,4 +43,17 @@
4043
<!-- Login Link -->
4144
<p class="text-center">Remember your password? <a href="{{ url_for('read_login') }}">Login here</a></p>
4245
</div>
46+
47+
<script>
48+
// JavaScript to validate password match
49+
document.getElementById('confirm_new_password').addEventListener('input', function() {
50+
const newPassword = document.getElementById('new_password').value;
51+
const confirmNewPassword = this.value;
52+
if (newPassword !== confirmNewPassword) {
53+
this.setCustomValidity('Passwords do not match.');
54+
} else {
55+
this.setCustomValidity('');
56+
}
57+
});
58+
</script>
4359
{% endblock %}

0 commit comments

Comments
 (0)