2019-07-05 12:47:35 +00:00
|
|
|
from flask import flash, redirect, render_template, request, url_for
|
2019-07-08 11:56:09 +00:00
|
|
|
from flask_login import current_user, login_required, login_user, logout_user
|
2019-07-04 13:17:51 +00:00
|
|
|
from . import auth
|
2019-07-08 09:27:54 +00:00
|
|
|
from .. import db
|
2019-07-09 09:00:41 +00:00
|
|
|
from .forms import ChangeProfileForm, LoginForm, PasswordResetForm, PasswordResetRequestForm, RegistrationForm
|
2019-07-08 09:04:52 +00:00
|
|
|
from ..email import send_email
|
2019-07-05 12:47:35 +00:00
|
|
|
from ..models import User
|
2019-07-04 13:17:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
@auth.route('/login', methods=['GET', 'POST'])
|
|
|
|
def login():
|
2019-07-05 12:47:35 +00:00
|
|
|
form = LoginForm()
|
|
|
|
if form.validate_on_submit():
|
2019-07-09 09:00:41 +00:00
|
|
|
user = User.query.filter_by(email=form.login.data).first()
|
|
|
|
if user is None:
|
|
|
|
user = User.query.filter_by(username=form.login.data).first()
|
2019-07-05 12:47:35 +00:00
|
|
|
if user is not None and user.verify_password(form.password.data):
|
|
|
|
login_user(user, form.remember_me.data)
|
|
|
|
next = request.args.get('next')
|
|
|
|
if next is None or not next.startswith('/'):
|
|
|
|
next = url_for('main.index')
|
|
|
|
return redirect(next)
|
|
|
|
flash('Invalid username or password.')
|
|
|
|
return render_template('auth/login.html.j2', form=form, title='Log in')
|
|
|
|
|
|
|
|
|
|
|
|
@auth.route('/logout')
|
|
|
|
@login_required
|
|
|
|
def logout():
|
|
|
|
logout_user()
|
|
|
|
flash('You have been logged out.')
|
|
|
|
return redirect(url_for('main.index'))
|
2019-07-04 13:17:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
@auth.route('/register', methods=['GET', 'POST'])
|
|
|
|
def register():
|
2019-07-08 12:04:02 +00:00
|
|
|
if not current_user.is_anonymous:
|
|
|
|
return redirect(url_for('main.index'))
|
2019-07-08 09:27:54 +00:00
|
|
|
form = RegistrationForm()
|
|
|
|
if form.validate_on_submit():
|
2019-07-08 13:58:46 +00:00
|
|
|
user = User(email=form.email.data.lower(),
|
|
|
|
username=form.username.data,
|
2019-07-08 09:27:54 +00:00
|
|
|
password=form.password.data)
|
|
|
|
db.session.add(user)
|
|
|
|
db.session.commit()
|
2019-07-08 13:58:46 +00:00
|
|
|
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.')
|
2019-07-08 09:27:54 +00:00
|
|
|
return redirect(url_for('auth.login'))
|
2019-07-08 12:04:02 +00:00
|
|
|
return render_template('auth/register.html.j2', form=form)
|
2019-07-08 09:04:52 +00:00
|
|
|
|
|
|
|
|
2019-07-08 13:58:46 +00:00
|
|
|
@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 confirmation link is invalid or has expired.')
|
|
|
|
return redirect(url_for('main.index'))
|
|
|
|
|
|
|
|
|
2019-07-09 08:09:35 +00:00
|
|
|
@auth.before_app_request
|
|
|
|
def before_request():
|
|
|
|
if current_user.is_authenticated \
|
|
|
|
and not current_user.confirmed \
|
|
|
|
and request.blueprint != 'auth' \
|
2019-07-09 11:18:46 +00:00
|
|
|
and request.endpoint != 'static':
|
2019-07-09 08:09:35 +00:00
|
|
|
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.j2')
|
|
|
|
|
|
|
|
|
|
|
|
@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 benn sent to you by email.')
|
2019-07-09 09:53:40 +00:00
|
|
|
return redirect(url_for('main.index'))
|
2019-07-09 08:09:35 +00:00
|
|
|
|
|
|
|
|
2019-07-08 09:04:52 +00:00
|
|
|
@auth.route('/reset', methods=['GET', 'POST'])
|
|
|
|
def password_reset_request():
|
2019-07-08 11:56:09 +00:00
|
|
|
if not current_user.is_anonymous:
|
|
|
|
return redirect(url_for('main.index'))
|
2019-07-08 09:04:52 +00:00
|
|
|
form = PasswordResetRequestForm()
|
|
|
|
if form.validate_on_submit():
|
|
|
|
user = User.query.filter_by(email=form.email.data.lower()).first()
|
|
|
|
if user:
|
|
|
|
token = user.generate_reset_token()
|
|
|
|
send_email(user.email, 'Reset Your Password',
|
|
|
|
'auth/email/reset_password',
|
|
|
|
user=user, token=token)
|
|
|
|
flash('An email with instructions to reset your password has been '
|
|
|
|
'sent to you.')
|
|
|
|
return redirect(url_for('auth.login'))
|
2019-07-08 11:56:09 +00:00
|
|
|
return render_template('auth/reset_password.html.j2', form=form,
|
|
|
|
title='Password Reset')
|
|
|
|
|
|
|
|
|
2019-07-08 13:13:32 +00:00
|
|
|
@auth.route('/reset/<token>', methods=['GET', 'POST'])
|
2019-07-08 11:56:09 +00:00
|
|
|
def password_reset(token):
|
2019-07-08 13:13:32 +00:00
|
|
|
if not current_user.is_anonymous:
|
|
|
|
return redirect(url_for('main.index'))
|
|
|
|
form = PasswordResetForm()
|
|
|
|
if form.validate_on_submit():
|
|
|
|
if User.reset_password(token, form.password.data):
|
|
|
|
db.session.commit()
|
|
|
|
flash('Your password has been updated.')
|
|
|
|
return redirect(url_for('auth.login'))
|
|
|
|
else:
|
|
|
|
return redirect(url_for('main.index'))
|
|
|
|
return render_template('auth/reset_password.html.j2', form=form,
|
|
|
|
title='Password Reset')
|
2019-07-09 09:00:41 +00:00
|
|
|
|
|
|
|
|
2019-07-09 11:40:33 +00:00
|
|
|
@auth.route('/account', methods=['GET', 'POST'])
|
2019-07-09 09:00:41 +00:00
|
|
|
@login_required
|
2019-07-09 11:40:33 +00:00
|
|
|
def account():
|
2019-07-09 09:00:41 +00:00
|
|
|
form = ChangeProfileForm()
|
|
|
|
if form.validate_on_submit():
|
2019-07-09 09:53:40 +00:00
|
|
|
flash('It is just a test, nothing changed.')
|
|
|
|
if form.username.data:
|
|
|
|
current_user.username = form.username.data
|
|
|
|
db.session.add(current_user)
|
|
|
|
if form.email.data:
|
|
|
|
current_user.email = form.email.data
|
|
|
|
current_user.confirmed = False
|
|
|
|
db.session.add(current_user)
|
2019-07-09 11:40:33 +00:00
|
|
|
resend_confirmation()
|
|
|
|
if form.password.data:
|
|
|
|
current_user.password = form.password.data
|
2019-07-09 09:53:40 +00:00
|
|
|
db.session.commit()
|
2019-07-09 11:40:33 +00:00
|
|
|
return redirect(url_for('auth.account'))
|
|
|
|
return render_template('auth/account.html.j2', form=form,
|
|
|
|
title='Account')
|