2022-02-08 11:26:20 +00:00
|
|
|
from app import db
|
|
|
|
from app.email import create_message, send
|
|
|
|
from app.models import User
|
2020-11-13 09:01:51 +00:00
|
|
|
from datetime import datetime
|
2022-02-08 11:26:20 +00:00
|
|
|
from flask import (
|
|
|
|
abort,
|
|
|
|
current_app,
|
|
|
|
flash,
|
|
|
|
redirect,
|
|
|
|
render_template,
|
|
|
|
request,
|
|
|
|
url_for
|
|
|
|
)
|
2020-02-19 15:24:58 +00:00
|
|
|
from flask_login import current_user, login_user, login_required, logout_user
|
2021-09-13 15:36:19 +00:00
|
|
|
from sqlalchemy import or_
|
2021-09-13 09:45:43 +00:00
|
|
|
from . import bp
|
2022-02-08 11:26:20 +00:00
|
|
|
from .forms import (
|
|
|
|
LoginForm,
|
|
|
|
ResetPasswordForm,
|
|
|
|
ResetPasswordRequestForm,
|
|
|
|
RegistrationForm
|
|
|
|
)
|
2020-02-19 15:24:58 +00:00
|
|
|
|
|
|
|
|
2021-09-13 09:45:43 +00:00
|
|
|
@bp.before_app_request
|
2020-02-19 15:24:58 +00:00
|
|
|
def before_request():
|
|
|
|
"""
|
|
|
|
Checks if a user is unconfirmed when visiting specific sites. Redirects to
|
|
|
|
unconfirmed view if user is unconfirmed.
|
|
|
|
"""
|
2020-04-27 11:50:54 +00:00
|
|
|
if current_user.is_authenticated:
|
2020-11-13 09:01:51 +00:00
|
|
|
current_user.last_seen = datetime.utcnow()
|
|
|
|
db.session.commit()
|
2022-02-08 11:26:20 +00:00
|
|
|
if (
|
|
|
|
not current_user.confirmed
|
|
|
|
and request.endpoint
|
|
|
|
and request.blueprint != 'auth'
|
|
|
|
and request.endpoint != 'static'
|
|
|
|
):
|
2020-04-27 11:50:54 +00:00
|
|
|
return redirect(url_for('auth.unconfirmed'))
|
2020-02-19 15:24:58 +00:00
|
|
|
|
|
|
|
|
2021-09-13 09:45:43 +00:00
|
|
|
@bp.route('/login', methods=['GET', 'POST'])
|
2020-02-19 15:24:58 +00:00
|
|
|
def login():
|
2020-02-19 15:58:56 +00:00
|
|
|
if current_user.is_authenticated:
|
|
|
|
return redirect(url_for('main.dashboard'))
|
2020-11-13 09:01:51 +00:00
|
|
|
form = LoginForm(prefix='login-form')
|
|
|
|
if form.validate_on_submit():
|
2022-02-08 11:26:20 +00:00
|
|
|
user = User.query.filter(
|
|
|
|
or_(
|
|
|
|
User.username == form.user.data,
|
|
|
|
User.email == form.user.data.lower()
|
|
|
|
)
|
|
|
|
).first()
|
2021-09-13 15:36:19 +00:00
|
|
|
if user and user.verify_password(form.password.data):
|
2020-11-13 09:01:51 +00:00
|
|
|
login_user(user, form.remember_me.data)
|
2020-02-19 15:24:58 +00:00
|
|
|
next = request.args.get('next')
|
|
|
|
if next is None or not next.startswith('/'):
|
|
|
|
next = url_for('main.dashboard')
|
|
|
|
return redirect(next)
|
2022-02-08 11:26:20 +00:00
|
|
|
flash('Invalid email/username or password', category='error')
|
2020-11-13 09:01:51 +00:00
|
|
|
return render_template('auth/login.html.j2', form=form, title='Log in')
|
2019-07-04 13:17:51 +00:00
|
|
|
|
|
|
|
|
2021-09-13 09:45:43 +00:00
|
|
|
@bp.route('/logout')
|
2019-07-05 12:47:35 +00:00
|
|
|
@login_required
|
|
|
|
def logout():
|
|
|
|
logout_user()
|
2022-02-08 11:26:20 +00:00
|
|
|
flash('You have been logged out')
|
2019-07-05 12:47:35 +00:00
|
|
|
return redirect(url_for('main.index'))
|
2019-07-04 13:17:51 +00:00
|
|
|
|
|
|
|
|
2021-09-13 09:45:43 +00:00
|
|
|
@bp.route('/register', methods=['GET', 'POST'])
|
2019-07-04 13:17:51 +00:00
|
|
|
def register():
|
2020-02-19 15:24:58 +00:00
|
|
|
if current_user.is_authenticated:
|
2019-08-02 11:24:52 +00:00
|
|
|
return redirect(url_for('main.dashboard'))
|
2020-11-13 09:01:51 +00:00
|
|
|
form = RegistrationForm(prefix='registration-form')
|
|
|
|
if form.validate_on_submit():
|
2022-02-03 11:39:16 +00:00
|
|
|
user = User(
|
|
|
|
email=form.email.data.lower(),
|
|
|
|
password=form.password.data,
|
|
|
|
username=form.username.data
|
|
|
|
)
|
2019-07-08 09:27:54 +00:00
|
|
|
db.session.add(user)
|
2022-02-03 11:39:16 +00:00
|
|
|
db.session.flush(objects=[user])
|
|
|
|
db.session.refresh(user)
|
2020-11-13 09:01:51 +00:00
|
|
|
try:
|
2022-02-03 11:39:16 +00:00
|
|
|
user.makedirs()
|
|
|
|
except OSError as e:
|
|
|
|
current_app.logger.error(e)
|
|
|
|
db.session.rollback()
|
2022-02-08 11:26:20 +00:00
|
|
|
flash('Internal Server Error', category='error')
|
2020-11-13 09:01:51 +00:00
|
|
|
abort(500)
|
2022-02-08 11:26:20 +00:00
|
|
|
token = user.generate_confirmation_token()
|
|
|
|
msg = create_message(
|
|
|
|
user.email,
|
|
|
|
'Confirm Your Account',
|
|
|
|
'auth/email/confirm',
|
|
|
|
token=token,
|
|
|
|
user=user
|
|
|
|
)
|
|
|
|
send(msg)
|
|
|
|
flash('A confirmation email has been sent to you by email')
|
|
|
|
db.session.commit()
|
|
|
|
return redirect(url_for('.login'))
|
2022-02-03 11:39:16 +00:00
|
|
|
return render_template(
|
|
|
|
'auth/register.html.j2',
|
|
|
|
form=form,
|
|
|
|
title='Register'
|
|
|
|
)
|
2019-07-08 09:04:52 +00:00
|
|
|
|
|
|
|
|
2021-09-13 09:45:43 +00:00
|
|
|
@bp.route('/confirm/<token>')
|
2019-07-08 13:58:46 +00:00
|
|
|
@login_required
|
|
|
|
def confirm(token):
|
|
|
|
if current_user.confirmed:
|
2020-02-19 15:24:58 +00:00
|
|
|
return redirect(url_for('main.dashboard'))
|
2019-07-08 13:58:46 +00:00
|
|
|
if current_user.confirm(token):
|
|
|
|
db.session.commit()
|
2022-02-08 11:26:20 +00:00
|
|
|
flash('You have confirmed your account')
|
2020-02-19 15:24:58 +00:00
|
|
|
return redirect(url_for('main.dashboard'))
|
2019-07-08 13:58:46 +00:00
|
|
|
else:
|
2022-02-08 11:26:20 +00:00
|
|
|
flash(
|
|
|
|
'The confirmation link is invalid or has expired',
|
|
|
|
category='error'
|
|
|
|
)
|
2020-11-13 09:01:51 +00:00
|
|
|
return redirect(url_for('.unconfirmed'))
|
2019-07-09 08:09:35 +00:00
|
|
|
|
|
|
|
|
2021-09-13 09:45:43 +00:00
|
|
|
@bp.route('/unconfirmed')
|
2019-07-09 08:09:35 +00:00
|
|
|
def unconfirmed():
|
2020-02-19 15:24:58 +00:00
|
|
|
if current_user.is_anonymous:
|
2019-07-09 08:09:35 +00:00
|
|
|
return redirect(url_for('main.index'))
|
2020-02-19 15:24:58 +00:00
|
|
|
elif current_user.confirmed:
|
|
|
|
return redirect(url_for('main.dashboard'))
|
2019-07-09 13:38:21 +00:00
|
|
|
return render_template('auth/unconfirmed.html.j2', title='Unconfirmed')
|
2019-07-09 08:09:35 +00:00
|
|
|
|
|
|
|
|
2021-09-13 09:45:43 +00:00
|
|
|
@bp.route('/confirm')
|
2019-07-09 08:09:35 +00:00
|
|
|
@login_required
|
|
|
|
def resend_confirmation():
|
|
|
|
token = current_user.generate_confirmation_token()
|
2022-02-08 11:26:20 +00:00
|
|
|
msg = create_message(
|
|
|
|
current_user.email,
|
|
|
|
'Confirm Your Account',
|
|
|
|
'auth/email/confirm',
|
|
|
|
token=token,
|
|
|
|
user=current_user
|
|
|
|
)
|
2020-04-21 16:34:21 +00:00
|
|
|
send(msg)
|
2022-02-08 11:26:20 +00:00
|
|
|
flash('A new confirmation email has been sent to you by email')
|
2020-02-19 15:24:58 +00:00
|
|
|
return redirect(url_for('auth.unconfirmed'))
|
2019-07-09 08:09:35 +00:00
|
|
|
|
|
|
|
|
2021-09-13 09:45:43 +00:00
|
|
|
@bp.route('/reset', methods=['GET', 'POST'])
|
2020-02-19 15:24:58 +00:00
|
|
|
def reset_password_request():
|
|
|
|
if current_user.is_authenticated:
|
2019-08-02 11:24:52 +00:00
|
|
|
return redirect(url_for('main.dashboard'))
|
2020-11-13 09:01:51 +00:00
|
|
|
form = ResetPasswordRequestForm(prefix='reset-password-request-form')
|
|
|
|
if form.validate_on_submit():
|
|
|
|
user = User.query.filter_by(email=form.email.data.lower()).first()
|
|
|
|
if user is not None:
|
2019-07-08 09:04:52 +00:00
|
|
|
token = user.generate_reset_token()
|
2022-02-08 11:26:20 +00:00
|
|
|
msg = create_message(
|
|
|
|
user.email,
|
|
|
|
'Reset Your Password',
|
|
|
|
'auth/email/reset_password',
|
|
|
|
token=token,
|
|
|
|
user=user
|
|
|
|
)
|
2020-04-21 16:34:21 +00:00
|
|
|
send(msg)
|
2022-02-08 11:26:20 +00:00
|
|
|
flash(
|
|
|
|
'An email with instructions to reset your password has been sent to you' # noqa
|
|
|
|
)
|
2020-11-13 09:01:51 +00:00
|
|
|
return redirect(url_for('.login'))
|
2022-02-08 11:26:20 +00:00
|
|
|
return render_template(
|
|
|
|
'auth/reset_password_request.html.j2',
|
|
|
|
form=form,
|
|
|
|
title='Password Reset'
|
|
|
|
)
|
2019-07-08 11:56:09 +00:00
|
|
|
|
|
|
|
|
2021-09-13 09:45:43 +00:00
|
|
|
@bp.route('/reset/<token>', methods=['GET', 'POST'])
|
2020-02-19 15:24:58 +00:00
|
|
|
def reset_password(token):
|
|
|
|
if current_user.is_authenticated:
|
2019-08-02 11:24:52 +00:00
|
|
|
return redirect(url_for('main.dashboard'))
|
2020-11-13 09:01:51 +00:00
|
|
|
form = ResetPasswordForm(prefix='reset-password-form')
|
|
|
|
if form.validate_on_submit():
|
|
|
|
if User.reset_password(token, form.password.data):
|
2019-07-08 13:13:32 +00:00
|
|
|
db.session.commit()
|
2022-02-08 11:26:20 +00:00
|
|
|
flash('Your password has been updated')
|
2020-11-13 09:01:51 +00:00
|
|
|
return redirect(url_for('.login'))
|
2019-07-08 13:13:32 +00:00
|
|
|
else:
|
|
|
|
return redirect(url_for('main.index'))
|
2022-02-08 11:26:20 +00:00
|
|
|
return render_template(
|
|
|
|
'auth/reset_password.html.j2',
|
|
|
|
form=form,
|
|
|
|
title='Password Reset',
|
|
|
|
token=token
|
|
|
|
)
|