|
@ -1,11 +1,28 @@ |
|
|
from flask import render_template, redirect, request, url_for, flash |
|
|
from flask import render_template, redirect, request, url_for, flash |
|
|
from flask_login import login_user, logout_user, login_required |
|
|
|
|
|
|
|
|
from flask_login import login_user, logout_user, login_required, current_user |
|
|
from . import auth |
|
|
from . import auth |
|
|
from .. import db |
|
|
from .. import db |
|
|
from ..models import User |
|
|
from ..models import User |
|
|
|
|
|
from ..email import send_email |
|
|
from .forms import LoginForm, RegistrationForm |
|
|
from .forms import LoginForm, RegistrationForm |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@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('/login', methods=['GET', 'POST']) |
|
|
@auth.route('/login', methods=['GET', 'POST']) |
|
|
def login(): |
|
|
def login(): |
|
|
form = LoginForm() |
|
|
form = LoginForm() |
|
@ -38,6 +55,32 @@ def register(): |
|
|
password=form.password.data) |
|
|
password=form.password.data) |
|
|
db.session.add(user) |
|
|
db.session.add(user) |
|
|
db.session.commit() |
|
|
db.session.commit() |
|
|
flash('You can now login.') |
|
|
|
|
|
return redirect(url_for('auth.login')) |
|
|
|
|
|
return render_template('auth/register.html', form=form) |
|
|
|
|
|
|
|
|
token = user.generate_confirmation_token() |
|
|
|
|
|
send_email(user.email, 'Confirm your account', |
|
|
|
|
|
'auth/email/confirm', user=user, token=token) |
|
|
|
|
|
flash('A confirmation email has been sent to you by email.') |
|
|
|
|
|
return redirect(url_for('main.index')) |
|
|
|
|
|
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 your account. Thanks!') |
|
|
|
|
|
else: |
|
|
|
|
|
flash('The confirmationlink is invalid or has expired') |
|
|
|
|
|
return redirect(url_for('main.index')) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@auth.route('/confirm') |
|
|
|
|
|
@login_required |
|
|
|
|
|
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')) |