2019-11-18 19:41:17 +01:00
|
|
|
from flask import request, flash, render_template, redirect, url_for
|
|
|
|
from flask_login import login_user
|
|
|
|
|
|
|
|
from app.auth.base import auth_bp
|
2021-10-12 14:36:47 +02:00
|
|
|
from app.db import Session
|
2024-02-29 11:20:29 +01:00
|
|
|
from app.log import LOG
|
2022-07-26 14:43:31 +02:00
|
|
|
from app.models import EmailChange, ResetPasswordCode
|
2019-11-18 19:41:17 +01:00
|
|
|
|
|
|
|
|
|
|
|
@auth_bp.route("/change_email", methods=["GET", "POST"])
|
|
|
|
def change_email():
|
|
|
|
code = request.args.get("code")
|
|
|
|
|
|
|
|
email_change: EmailChange = EmailChange.get_by(code=code)
|
|
|
|
|
|
|
|
if not email_change:
|
2020-08-13 10:59:39 +02:00
|
|
|
return render_template("auth/change_email.html")
|
2019-11-18 19:41:17 +01:00
|
|
|
|
|
|
|
if email_change.is_expired():
|
2020-08-13 10:59:39 +02:00
|
|
|
# delete the expired email
|
|
|
|
EmailChange.delete(email_change.id)
|
2021-10-12 14:36:47 +02:00
|
|
|
Session.commit()
|
2020-08-13 10:59:39 +02:00
|
|
|
return render_template("auth/change_email.html")
|
2019-11-18 19:41:17 +01:00
|
|
|
|
|
|
|
user = email_change.user
|
2024-02-29 11:20:29 +01:00
|
|
|
old_email = user.email
|
2019-11-18 19:41:17 +01:00
|
|
|
user.email = email_change.new_email
|
|
|
|
|
|
|
|
EmailChange.delete(email_change.id)
|
2022-07-26 14:43:31 +02:00
|
|
|
ResetPasswordCode.filter_by(user_id=user.id).delete()
|
2021-10-12 14:36:47 +02:00
|
|
|
Session.commit()
|
2019-11-18 19:41:17 +01:00
|
|
|
|
2024-02-29 11:20:29 +01:00
|
|
|
LOG.i(f"User {user} has changed their email from {old_email} to {user.email}")
|
2019-12-30 17:43:38 +01:00
|
|
|
flash("Your new email has been updated", "success")
|
2019-11-18 19:41:17 +01:00
|
|
|
|
|
|
|
login_user(user)
|
|
|
|
|
|
|
|
return redirect(url_for("dashboard.index"))
|