From 479737e7ae5d6d37506441edc67a30fe94d9567c Mon Sep 17 00:00:00 2001 From: lesliebinbin <506120340@qq.com> Date: Thu, 23 Aug 2018 20:07:44 +1000 Subject: [PATCH] add confirmation email function --- app/auth/views.py | 44 ++++++++++++++++++++++++-- app/models.py | 2 +- app/templates/auth/email/confirm.html | 8 +++++ app/templates/auth/email/confirm.txt | 7 ++++ app/templates/auth/register.html | 13 ++++++++ app/templates/auth/unconfirmed.html | 20 ++++++++++++ data-dev.sqlite | Bin 32768 -> 32768 bytes 7 files changed, 91 insertions(+), 3 deletions(-) create mode 100644 app/templates/auth/email/confirm.html create mode 100644 app/templates/auth/email/confirm.txt create mode 100644 app/templates/auth/unconfirmed.html diff --git a/app/auth/views.py b/app/auth/views.py index f7c2f30..e3f1896 100644 --- a/app/auth/views.py +++ b/app/auth/views.py @@ -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']) @@ -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) \ No newline at end of file + return render_template('auth/register.html', form=form) + + +@auth.route('/confirm/') +@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')) diff --git a/app/models.py b/app/models.py index 0c87568..e2a7af7 100755 --- a/app/models.py +++ b/app/models.py @@ -58,4 +58,4 @@ def __repr__(self): @login_manager.user_loader def load_user(user_id): - return User.query.get(int(user_id)) \ No newline at end of file + return User.query.get(int(user_id)) diff --git a/app/templates/auth/email/confirm.html b/app/templates/auth/email/confirm.html new file mode 100644 index 0000000..1909070 --- /dev/null +++ b/app/templates/auth/email/confirm.html @@ -0,0 +1,8 @@ +

Dear {{user.username}}

+

Welcome to Flasky!

+

To confirm your account please click here.

+

Alternatively, you can paste the following link in your browser's address bar:

+

{{url_for('auth.confirm', token=token, _external=True)}}

+

Sincerely,

+

The Flasky Team

+

Note: replies to this email address are not monitored.

\ No newline at end of file diff --git a/app/templates/auth/email/confirm.txt b/app/templates/auth/email/confirm.txt new file mode 100644 index 0000000..8b22f81 --- /dev/null +++ b/app/templates/auth/email/confirm.txt @@ -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. \ No newline at end of file diff --git a/app/templates/auth/register.html b/app/templates/auth/register.html index e69de29..879bead 100644 --- a/app/templates/auth/register.html +++ b/app/templates/auth/register.html @@ -0,0 +1,13 @@ +{% extends "base.html" %} +{%import "bootstrap/wtf.html" as wtf%} +{% block title %} + Flasky - Register +{% endblock title %} +{% block page_content %} + +
+ {{wtf.quick_form(form)}} +
+{% endblock page_content %} \ No newline at end of file diff --git a/app/templates/auth/unconfirmed.html b/app/templates/auth/unconfirmed.html new file mode 100644 index 0000000..5c96009 --- /dev/null +++ b/app/templates/auth/unconfirmed.html @@ -0,0 +1,20 @@ +{% extends "base.html" %} +{% block title %} + Flasky - Confirm your account +{% endblock title %} +{% block page_content %} + +{% endblock page_content %} \ No newline at end of file diff --git a/data-dev.sqlite b/data-dev.sqlite index 78d76a2049283b56209413d3026036df1b7742bd..66950dc57a4de0cbdfe6ca04dffc0327d8985531 100644 GIT binary patch delta 324 zcmZo@U}|V!njp<6JyFJ)QF>#-5`Q^f-sKGZZ~0I1ujKFNFX#8=*XR4e_mJ;0-!?ui z-sPJG1zLIQ8yR>x7?c?r-E}#mGBT4g^E^ru^U@2FvQyHGtco)djZDp~ObviQ#i!Ie z*{>?uUB%qc$RH)jI5jEB#30SsGRf2|)gU>^!o=Ld)YvfDEGa2D+0wwo(9$5qz$nSm zEIBR3BGJG+%{ax}A_>T}Ff}j(nqX{V;80k|ua}&k+sMho!5|5AgD2-^)421Da=d)2 z8Tiflj`2Oc5 delta 142 zcmV;90CE3-fC7Mk0+1U47?B)90T{7hq)!|S4l4i;?GMQhsSl42cMnYuFAn$);SSRd zwhk+^5fBRw3V{d=2LKrWfk#QRHD1pF6buf#01r0~#tz^Q?+=s@eGgO*%(EI`kq(iN w60^H*NdX!R52pYR{15jJ@(=9~=nvx$-VfOi)DO=O%CjJFau1P^7n7%dS