-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add email verification and password reset to flask backend
- Loading branch information
1 parent
aa3dfe7
commit bc7c5fa
Showing
12 changed files
with
170 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from threading import Thread | ||
from flask import current_app, render_template | ||
from flask_mail import Message | ||
from app import mail | ||
|
||
def send_async_email(app, msg): | ||
with app.app_context(): | ||
mail.send(msg) | ||
|
||
def send_email(subject, sender, recipients, text_body, html_body, attachments=None, sync=False): | ||
msg = Message(subject, sender=sender, recipients=recipients) | ||
msg.body = text_body | ||
msg.html = html_body | ||
if attachments: | ||
for attachment in attachments: | ||
msg.attach(*attachment) | ||
if sync: | ||
mail.send(msg) | ||
else: | ||
Thread(target=send_async_email, args=(current_app._get_current_object(), msg)).start() | ||
|
||
def send_verification_email(user): | ||
token = user.get_email_verification_token() | ||
send_email('[Totlahtol] Verify Your Email', | ||
sender=current_app.config['ADMINS'][0], | ||
recipients=[user.email], | ||
text_body=render_template('verify_email.txt', | ||
user=user, token=token), | ||
html_body=render_template('verify_email.html', | ||
user=user, token=token)) | ||
|
||
def send_password_reset_email(user): | ||
token = user.get_reset_password_token() | ||
send_email('[Totlahtol] Reset Your Password', | ||
sender=current_app.config['ADMINS'][0], | ||
recipients=[user.email], | ||
text_body=render_template('reset_password.txt', | ||
user=user, token=token), | ||
html_body=render_template('reset_password.html', | ||
user=user, token=token)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<p>Dear {{ user.username }},</p> | ||
<p> | ||
To reset your password | ||
<a href="{{ url_for('reset_password', token=token, _external=True) }}"> | ||
click here | ||
</a>. | ||
</p> | ||
<p>Alternatively, you can paste the following link in your browser's address bar:</p> | ||
<p>{{ url_for('reset_password', token=token, _external=True) }}</p> | ||
<p>If you have not requested a password reset, simply ignore this message.</p> | ||
<p>Sincerely,</p> | ||
<p>The Totlahtol Team</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
Dear {{ user.username }}, | ||
|
||
To reset your password click on the following link: | ||
|
||
{{ url_for('api.reset_password', token=token, _external=True) }} | ||
|
||
If you have not requested a password reset simply ignore this message. | ||
|
||
Sincerely, | ||
|
||
The Microblog Team |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<p>Dear {{ user.username }},</p> | ||
<p> | ||
To verify your email | ||
<a href="{{ url_for('api.verify_user_email', token=token, _external=True) }}"> | ||
click here | ||
</a>. | ||
</p> | ||
<p>Alternatively, you can paste the following link in your browser's address bar:</p> | ||
<p>{{ url_for('api.verify_user_email', token=token, _external=True) }}</p> | ||
<p>If you have not signed up for our app, simply ignore this message.</p> | ||
<p>Sincerely,</p> | ||
<p>The Totlahtol Team</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
Dear {{ user.username }}, | ||
|
||
To verify your email click on the following link: | ||
|
||
{{ url_for('api.verify_user_email', token=token, _external=True) }} | ||
|
||
If you have not signed up for our app, simply ignore this message. | ||
|
||
Sincerely, | ||
|
||
The Microblog Team |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,4 +22,5 @@ class Config(object): | |
ADMINS = ['[email protected]'] | ||
# POSTS_PER_PAGE = 3 | ||
# LANGUAGES = ['en', 'es'] | ||
# MS_TRANSLATOR_KEY=os.environ.get('MS_TRANSLATOR_KEY') | ||
# MS_TRANSLATOR_KEY=os.environ.get('MS_TRANSLATOR_KEY') | ||
FRONTEND_URL = os.environ.get('TOTLAHTOL_FRONTEND_URL') or '/' |
32 changes: 32 additions & 0 deletions
32
api/migrations/versions/5171de0b0403_add_verified_field_to_user_model.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
"""add 'verified' field to User model | ||
Revision ID: 5171de0b0403 | ||
Revises: 5d988c97b3b7 | ||
Create Date: 2021-01-02 22:52:45.718436 | ||
""" | ||
from alembic import op | ||
import sqlalchemy as sa | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
revision = '5171de0b0403' | ||
down_revision = '5d988c97b3b7' | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
with op.batch_alter_table('user', schema=None) as batch_op: | ||
batch_op.add_column(sa.Column('verified', sa.Boolean(), nullable=True)) | ||
|
||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
with op.batch_alter_table('user', schema=None) as batch_op: | ||
batch_op.drop_column('verified') | ||
|
||
# ### end Alembic commands ### |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters