create change_email view
This commit is contained in:
parent
be6266fb0e
commit
2dd25cc72d
|
@ -9,4 +9,5 @@ from .views import (
|
|||
github,
|
||||
google,
|
||||
facebook,
|
||||
change_email,
|
||||
)
|
||||
|
|
|
@ -0,0 +1,30 @@
|
|||
{% extends "single.html" %}
|
||||
|
||||
{% block title %}
|
||||
Change Email
|
||||
{% endblock %}
|
||||
|
||||
{% block single_content %}
|
||||
{% if error %}
|
||||
<div class="text-danger text-center mb-4">{{ error }}</div>
|
||||
{% endif %}
|
||||
|
||||
{% if incorrect_code %}
|
||||
<div class="text-danger text-center h4">
|
||||
The link is incorrect. <br><br>
|
||||
Please go to <a href="{{ url_for('dashboard.setting') }}"
|
||||
class="btn btn-warning">settings</a>
|
||||
page to re-send confirmation email.
|
||||
</div>
|
||||
{% endif %}
|
||||
|
||||
{% if expired_code %}
|
||||
<div class="text-danger text-center h4">
|
||||
The link is already expired. <br><br>
|
||||
Please go to <a href="{{ url_for('dashboard.setting') }}"
|
||||
class="btn btn-warning">settings</a>
|
||||
page to re-send confirmation email.
|
||||
</div>
|
||||
{% endif %}
|
||||
|
||||
{% endblock %}
|
|
@ -0,0 +1,31 @@
|
|||
from flask import request, flash, render_template, redirect, url_for
|
||||
from flask_login import login_user
|
||||
|
||||
from app.auth.base import auth_bp
|
||||
from app.extensions import db
|
||||
from app.models import EmailChange
|
||||
|
||||
|
||||
@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:
|
||||
return render_template("auth/change_email.html", incorrect_code=True)
|
||||
|
||||
if email_change.is_expired():
|
||||
return render_template("auth/change_email.html", expired_code=True)
|
||||
|
||||
user = email_change.user
|
||||
user.email = email_change.new_email
|
||||
|
||||
EmailChange.delete(email_change.id)
|
||||
db.session.commit()
|
||||
|
||||
flash("Your new email has been updated successfully", "success")
|
||||
|
||||
login_user(user)
|
||||
|
||||
return redirect(url_for("dashboard.index"))
|
Loading…
Reference in New Issue