aboutsummaryrefslogtreecommitdiff
path: root/webmentions_ssg/views/user.py
blob: 4675ade4bab9a8f4f075d065838054afc8d45143 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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)