Add feature user can change mail address

This commit is contained in:
2026-09-01 08:39:30 +03:00
parent f2ff00894e
commit 2dc4dedb2b
4 changed files with 42 additions and 2 deletions
+24
View File
@@ -406,6 +406,30 @@ def change_password():
return render_template("password_form.html", mode="change")
@app.route("/account/email", methods=("GET", "POST"))
@login_required
def change_email():
user = get_db().execute("SELECT email, password_hash FROM users WHERE id = ?", (session["user_id"],)).fetchone()
if request.method == "POST":
email = request.form.get("email", "").strip().lower()
current_password = request.form.get("current_password", "")
if not check_password_hash(user["password_hash"], current_password):
flash("Your current password is incorrect.", "error")
elif not valid_email(email):
flash("Enter a valid email address.", "error")
elif email == user["email"]:
flash("That is already your email address.", "notice")
else:
try:
get_db().execute("UPDATE users SET email = ? WHERE id = ?", (email, session["user_id"]))
get_db().commit()
flash("Email address updated.", "success")
return redirect(url_for("index"))
except sqlite3.IntegrityError:
flash("That email address is already in use.", "error")
return render_template("email_form.html", email=user["email"])
@app.post("/logout")
def logout():
session.clear()