From 7b27c155158757a542a17eb97cc73e507194fb16 Mon Sep 17 00:00:00 2001 From: Colin Maudry Date: Mon, 20 Apr 2026 22:52:51 +0200 Subject: [PATCH] =?UTF-8?q?src/pages/compte.py=20:=20page=20mon=20compte?= =?UTF-8?q?=20(prot=C3=A9g=C3=A9e)=20(#73)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pages/compte.py | 92 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 src/pages/compte.py diff --git a/src/pages/compte.py b/src/pages/compte.py new file mode 100644 index 0000000..f676196 --- /dev/null +++ b/src/pages/compte.py @@ -0,0 +1,92 @@ +import dash_bootstrap_components as dbc +from dash import Input, Output, callback, dcc, html, register_page +from flask_login import current_user +from flask_wtf.csrf import generate_csrf + +NAME = "Mon compte" + +register_page( + __name__, + path="/compte", + title="Mon compte | decp.info", + name=NAME, + description="Page de gestion de compte.", +) + +ERROR_MESSAGES = { + "invalid_current_password": "Le mot de passe actuel est incorrect.", + "password_too_short": "Le nouveau mot de passe doit faire au moins 8 caractères.", + "password_mismatch": "Les nouveaux mots de passe ne correspondent pas.", +} + + +def layout(error: str | None = None, password_changed: str | None = None, **_): + if not current_user.is_authenticated: + return dcc.Location(href="/connexion?next=/compte", id="compte-redirect") + + alerts = [] + if error and error in ERROR_MESSAGES: + alerts.append(dbc.Alert(ERROR_MESSAGES[error], color="danger")) + if password_changed == "1": + alerts.append(dbc.Alert("Mot de passe mis à jour.", color="success")) + + return dbc.Container( + className="py-4", + style={"maxWidth": "600px"}, + children=[ + html.H2("Mon compte"), + html.P([html.Strong("Email : "), current_user.email]), + *alerts, + html.H4("Changer le mot de passe", className="mt-4"), + html.Form( + method="POST", + action="/auth/change-password", + children=[ + dcc.Input(type="hidden", id="csrf-change", name="csrf_token"), + dbc.Label("Mot de passe actuel"), + dbc.Input( + type="password", + name="current_password", + required=True, + className="mb-3", + ), + dbc.Label("Nouveau mot de passe (8 caractères minimum)"), + dbc.Input( + type="password", + name="password", + required=True, + minLength=8, + className="mb-3", + ), + dbc.Label("Confirmer le nouveau mot de passe"), + dbc.Input( + type="password", + name="password_confirm", + required=True, + minLength=8, + className="mb-3", + ), + dbc.Button("Changer", type="submit", color="primary"), + ], + ), + html.Hr(className="mt-5"), + html.Form( + method="POST", + action="/auth/logout", + children=[ + dcc.Input(type="hidden", id="csrf-logout", name="csrf_token"), + dbc.Button("Déconnexion", type="submit", color="secondary"), + ], + ), + ], + ) + + +@callback(Output("csrf-change", "value"), Input("csrf-change", "id")) +def _fill_csrf_change(_): + return generate_csrf() + + +@callback(Output("csrf-logout", "value"), Input("csrf-logout", "id")) +def _fill_csrf_logout(_): + return generate_csrf()