diff options
| author | Dennis Fink | 2026-11-17 21:23:57 +0100 |
|---|---|---|
| committer | Dennis Fink | 2026-11-17 21:23:57 +0100 |
| commit | acd1f42847786963fc4aaeed884c3091700c9e35 (patch) | |
| tree | 5a126bdfd96e69c985137bb2a3db8d9066ecd12d /webmentions_ssg/views/user.py | |
| parent | c4c17e3e046a627a7d2353c734bc69f05d4fc3b1 (diff) | |
| download | webmentions-ssg-acd1f42847786963fc4aaeed884c3091700c9e35.tar.gz webmentions-ssg-acd1f42847786963fc4aaeed884c3091700c9e35.zip | |
feat(core): add user account page
Add an authenticated user page showing the current username and remote
address, including guidance for validating reverse-proxy hop
configuration.
Allow users to change their own password through a CSRF-protected
WTForms form while preserving the active session and invalidating other
existing sessions.
Diffstat (limited to 'webmentions_ssg/views/user.py')
| -rw-r--r-- | webmentions_ssg/views/user.py | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/webmentions_ssg/views/user.py b/webmentions_ssg/views/user.py new file mode 100644 index 0000000..4675ade --- /dev/null +++ b/webmentions_ssg/views/user.py @@ -0,0 +1,39 @@ +# SPDX-FileCopyrightText: 2026 Dennis Fink <me+coding@dennisfink.me> +# +# SPDX-License-Identifier: BSD-3-Clause + +"""Authenticated user account page.""" + +from typing import cast + +from flask import flash, redirect, render_template, request, url_for +from flask.typing import ResponseReturnValue +from flask_login import current_user, login_required, login_user + +from ..extensions import db +from ..forms.user import ChangePasswordForm +from ..models.user import User +from .blueprint import webmentions_bp + + +@webmentions_bp.route("/user", methods=["GET", "POST"]) +@login_required +def user_page() -> ResponseReturnValue: + """Display account details and let the signed-in user change their password.""" + form = ChangePasswordForm() + + if form.validate_on_submit(): + user = cast(User, current_user._get_current_object()) + if not user.check_password(form.current_password.data or ""): + form.current_password.errors.append("Current password is incorrect.") + else: + user.password = form.new_password.data or "" + db.session.commit() + + # Password changes revoke all old session identifiers. Refresh this + # session with the new fingerprint so the user stays signed in. + login_user(user) + flash("Password changed successfully.", "success") + return redirect(url_for("webmentions.user_page")) + + return render_template("user.html", form=form, remote_address=request.remote_addr) |
