Skip to content

Commit

Permalink
add confirmation email function
Browse files Browse the repository at this point in the history
  • Loading branch information
lesliebinbin committed Aug 23, 2018
1 parent 3643bbf commit 479737e
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 3 deletions.
44 changes: 42 additions & 2 deletions app/auth/views.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from flask import render_template, redirect, request, url_for, flash
from . import auth
from flask_login import login_user, logout_user, login_required
from flask_login import login_user, logout_user, login_required, current_user
from ..models import User
from .forms import LoginForm, RegistrationForm
from .. import db
from ..email import send_email


@auth.route('/login', methods=['GET', 'POST'])
Expand Down Expand Up @@ -41,4 +42,43 @@ def register():
db.session.commit()
flash('You can now login.')
return redirect(url_for('auth.login'))
return render_template('auth/register.html', form=form)
return render_template('auth/register.html', form=form)


@auth.route('/confirm/<token>')
@login_required
def confirm(token):
if current_user.confirmed:
return redirect(url_for('main.index'))
if current_user.confirm(token):
db.session.commit()
flash('You have confirmed you account.Cheers!')
else:
flash('The confirmation link is invalid or has expired.')
return redirect(url_for('main.index'))


@auth.before_app_request
def before_request():
if current_user.is_authenticated and not current_user.confirmed and request.blueprint != 'auth' and request.endpoint != 'static':
return redirect(url_for('auth.unconfirmed'))


@auth.route('/unconfirmed')
def unconfirmed():
if current_user.is_anonymous or current_user.confirmed:
return redirect(url_for('main.index'))
return render_template('auth/unconfirmed.html')


@auth.route('/resend_confirmation')
def resend_confirmation():
token = current_user.generate_confirmation_token()
send_email(
[current_user.email],
'Confirm Your Account',
'auth/email/confirm',
user=current_user,
token=token)
flash('A new confirmation email has been sent to you by email')
return redirect(url_for('main.index'))
2 changes: 1 addition & 1 deletion app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,4 @@ def __repr__(self):

@login_manager.user_loader
def load_user(user_id):
return User.query.get(int(user_id))
return User.query.get(int(user_id))
8 changes: 8 additions & 0 deletions app/templates/auth/email/confirm.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<p>Dear {{user.username}}</p>
<p>Welcome to <b>Flasky</b>!</p>
<p>To confirm your account please <a href="{{url_for('auth.confirm', 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('auth.confirm', token=token, _external=True)}}</p>
<p>Sincerely,</p>
<p>The Flasky Team</p>
<p><small>Note: replies to this email address are not monitored.</small></p>
7 changes: 7 additions & 0 deletions app/templates/auth/email/confirm.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Dear {{user.username}}
Welcome to Flasky!
To confirm you account please click on the following link:
{{url_for('auth.confirm', token=token, _external=True)}}
Sincerely,
The Flasky Team
Note: replies to this email address are not monitored.
13 changes: 13 additions & 0 deletions app/templates/auth/register.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{% extends "base.html" %}
{%import "bootstrap/wtf.html" as wtf%}
{% block title %}
Flasky - Register
{% endblock title %}
{% block page_content %}
<div class="page-header">
<h1>Register</h1>
</div>
<div class="col-md-4">
{{wtf.quick_form(form)}}
</div>
{% endblock page_content %}
20 changes: 20 additions & 0 deletions app/templates/auth/unconfirmed.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{% extends "base.html" %}
{% block title %}
Flasky - Confirm your account
{% endblock title %}
{% block page_content %}
<div class="page-header">
<h1>
Hello, {{ current_user.username }}!
</h1>
<h3>You have not confirmed your account yet.</h3>
<p>
Before you can access this site you need to confirm your account.
Check your inbox, you should have received an email with a confirmation link.
</p>
<p>
Need another confirmation email?
<a href="{{ url_for('auth.resend_confirmation') }}">Click here</a>
</p>
</div>
{% endblock page_content %}
Binary file modified data-dev.sqlite
Binary file not shown.

0 comments on commit 479737e

Please sign in to comment.