|
8 | 8 | from .forms import LoginForm, RegistrationForm |
9 | 9 |
|
10 | 10 |
|
| 11 | +@auth.before_app_request |
| 12 | +def before_request(): |
| 13 | + if current_user.is_authenticated \ |
| 14 | + and not current_user.confirmed \ |
| 15 | + and request.endpoint \ |
| 16 | + and request.blueprint != 'auth' \ |
| 17 | + and request.endpoint != 'static': |
| 18 | + return redirect(url_for('auth.unconfirmed')) |
| 19 | + |
| 20 | + |
| 21 | +@auth.route('/unconfirmed') |
| 22 | +def unconfirmed(): |
| 23 | + if current_user.is_anonymous or current_user.confirmed: |
| 24 | + return redirect(url_for('main.index')) |
| 25 | + return render_template('auth/unconfirmed.html') |
| 26 | + |
| 27 | + |
11 | 28 | @auth.route('/login', methods=['GET', 'POST']) |
12 | 29 | def login(): |
13 | 30 | form = LoginForm() |
@@ -40,6 +57,32 @@ def register(): |
40 | 57 | password=form.password.data) |
41 | 58 | db.session.add(user) |
42 | 59 | db.session.commit() |
43 | | - flash('You can now login.') |
| 60 | + token = user.generate_confirmation_token() |
| 61 | + send_email(user.email, 'Confirm Your Account', |
| 62 | + 'auth/email/confirm', user=user, token=token) |
| 63 | + flash('A confirmation email has been sent to you by email.') |
44 | 64 | return redirect(url_for('auth.login')) |
45 | 65 | return render_template('auth/register.html', form=form) |
| 66 | + |
| 67 | + |
| 68 | +@auth.route('/confirm/<token>') |
| 69 | +@login_required |
| 70 | +def confirm(token): |
| 71 | + if current_user.confirmed: |
| 72 | + return redirect(url_for('main.index')) |
| 73 | + if current_user.confirm(token): |
| 74 | + db.session.commit() |
| 75 | + flash('You have confirmed your account. Thanks!') |
| 76 | + else: |
| 77 | + flash('The confirmation link is invalid or has expired.') |
| 78 | + return redirect(url_for('main.index')) |
| 79 | + |
| 80 | + |
| 81 | +@auth.route('/confirm') |
| 82 | +@login_required |
| 83 | +def resend_confirmation(): |
| 84 | + token = current_user.generate_confirmation_token() |
| 85 | + send_email(current_user.email, 'Confirm Your Account', |
| 86 | + 'auth/email/confirm', user=current_user, token=token) |
| 87 | + flash('A new confirmation email has been sent to you by email.') |
| 88 | + return redirect(url_for('main.index')) |
0 commit comments