Skip to content
This repository has been archived by the owner on Nov 4, 2022. It is now read-only.

Commit

Permalink
Create nice password reset emails
Browse files Browse the repository at this point in the history
with branding support for NCCRD - ref #19
  • Loading branch information
marksparkza committed Jul 15, 2021
1 parent e4c0165 commit 8b21423
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 6 deletions.
6 changes: 6 additions & 0 deletions odp/identity/templates/email/reset_password.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{% extends 'email/base_message.html' %}

{% block content %}
<p>A request has been made to reset your password. To confirm this reset request, please go to:</p>
<p><a href="{{ url }}">{{ url }}</a></p>
{% endblock %}
6 changes: 6 additions & 0 deletions odp/identity/templates/email/reset_password.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{% extends 'email/base_message.txt' %}

{%- block content -%}
A request has been made to reset your password. To confirm this reset request, please go to:
{{ url }}
{%- endblock -%}
5 changes: 5 additions & 0 deletions odp/identity/templates/email/reset_password_subject.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{% extends 'email/base_subject.txt' %}

{%- block subject -%}
Reset your password
{%- endblock -%}
17 changes: 12 additions & 5 deletions odp/identity/views/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,21 +210,28 @@ def send_verification_email(email, name, challenge, brand):
flash("There was a problem sending the verification email.", category='error')


def send_password_reset_email(email, challenge, brand):
def send_password_reset_email(email, name, challenge, brand):
"""Send a password reset email.
:param email: the email address
:param name: the user's full name
:param challenge: the Hydra login challenge
:param brand: branding identifier
"""
try:
token = encode_token('account.reset_password', challenge, brand, email=email)
reset_url = url_for('account.reset_password', token=token, _external=True)
context = {
'url': url_for('account.reset_password', token=token, _external=True),
'name': name,
'brand': brand,
}
msg = Message(
subject="SAEON Open Data Platform: Request to reset your password",
body="Click the following link to reset your password: " + reset_url,
sender=("SAEON", "noreply@saeon.ac.za"),
subject=render_template('email/reset_password_subject.txt', **context),
body=render_template('email/reset_password.txt', **context),
html=render_template('email/reset_password.html', **context),
recipients=[email],
sender=("SAEON", "noreply@saeon.ac.za"),
reply_to=("SAEON", "noreply@saeon.ac.za"),
)
mail.send(msg)
flash("A password reset link has been sent to your email address.")
Expand Down
3 changes: 2 additions & 1 deletion odp/identity/views/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ def forgot_password():
email = form.email.data
try:
user_id = validate_forgot_password(email)
send_password_reset_email(email, challenge, brand)
name = get_user_profile_by_email(email)['name']
send_password_reset_email(email, name, challenge, brand)
sent = True

except x.ODPUserNotFound:
Expand Down

0 comments on commit 8b21423

Please sign in to comment.