diff --git a/TODO.md b/TODO.md index c26a15c..b65ddc7 100644 --- a/TODO.md +++ b/TODO.md @@ -12,6 +12,7 @@ - [x] Add validated post image uploads with an in-article preview and click-to-expand view. - [x] Add ordered inline step images with captions for long-form repair and engineering posts. - [ ] Add code snippets to long-form stories. -- [ ] Allow users to change their email addresses. +- [x] Allow users to change their email addresses. - [ ] Add user email verification for registration and email-address changes. -- [ ] Add user real names and use them as the published-by name. \ No newline at end of file +- [ ] Add user real names and use them as the published-by name. +- [ ] Add ability to hide posts from unregistered users. \ No newline at end of file diff --git a/app.py b/app.py index f080cdf..2826882 100644 --- a/app.py +++ b/app.py @@ -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() diff --git a/templates/base.html b/templates/base.html index 353fe40..d0d0678 100644 --- a/templates/base.html +++ b/templates/base.html @@ -21,6 +21,7 @@ {% if current_user %} Write {% if current_user.role == 'admin' %}Accounts{% endif %} + Email Password {{ current_user.username }}
diff --git a/templates/email_form.html b/templates/email_form.html new file mode 100644 index 0000000..a6f880d --- /dev/null +++ b/templates/email_form.html @@ -0,0 +1,14 @@ +{% extends 'base.html' %} +{% block title %}Email address | Eternity Project{% endblock %} +{% block content %} +
+
ACCOUNT DETAILS

Update your
address.

Use an email address you control. Confirm your current password before saving the change.

+
+ + + + + +
+
+{% endblock %} \ No newline at end of file