Skip to content

Commit 52fa67a

Browse files
committed
Improved change password page
1 parent 8475244 commit 52fa67a

File tree

5 files changed

+154
-196
lines changed

5 files changed

+154
-196
lines changed

backend/views/core/settings/view.py

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -81,31 +81,39 @@ def settings_page(request: HttpRequest):
8181

8282
def change_password(request: HttpRequest):
8383
if request.method == "POST":
84-
error: str = ""
85-
84+
current_password = request.POST.get("current_password")
8685
password = request.POST.get("password")
8786
confirm_password = request.POST.get("confirm_password")
8887

89-
if password != confirm_password:
90-
error = "Passwords don't match"
91-
92-
if not password:
93-
error = "Something went wrong, no password was provided."
94-
95-
if not error and len(password) > 128:
96-
error = "Password either too short, or too long. Minimum characters is eight, maximum is 128."
97-
98-
if not error and len(password) < 8:
99-
error = "Password either too short, or too long. Minimum characters is eight, maximum is 128."
88+
error = validate_password_change(
89+
request.user, current_password, password, confirm_password
90+
)
10091

10192
if error:
10293
messages.error(request, error)
10394
return redirect("user settings change_password")
10495

96+
# If no errors, update the password
10597
request.user.set_password(password)
10698
request.user.save()
10799
update_session_auth_hash(request, request.user)
108100
messages.success(request, "Successfully changed your password.")
109101
return redirect("user settings")
110102

111103
return render(request, "pages/reset_password.html", {"type": "change"})
104+
105+
106+
def validate_password_change(user, current_password, new_password, confirm_password):
107+
if not user.check_password(current_password):
108+
return "Incorrect current password"
109+
110+
if new_password != confirm_password:
111+
return "Passwords don't match"
112+
113+
if not new_password:
114+
return "Something went wrong, no password was provided."
115+
116+
if len(new_password) < 8 or len(new_password) > 128:
117+
return "Password must be between 8 and 128 characters."
118+
119+
return None

frontend/static/src/input.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
.avatar-img-ring {
2727
@apply ring;
2828
@apply ring-primary;
29+
@apply focus:ring-offset-4;
2930
@apply hover:ring-offset-4;
3031
@apply ring-offset-base-100;
3132
@apply ring-offset-2;

0 commit comments

Comments
 (0)