nopaque/web/app/auth/views.py

158 lines
6.0 KiB
Python
Raw Normal View History

2019-11-14 09:01:45 +00:00
from flask import (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
from . import auth
2020-02-19 15:24:58 +00:00
from .forms import (LoginForm, ResetPasswordForm, ResetPasswordRequestForm,
RegistrationForm)
2020-03-27 11:06:11 +00:00
from .. import db
from ..email import create_message, send
2020-03-27 11:06:11 +00:00
from ..models import User
2019-11-14 09:01:45 +00:00
import os
2020-02-19 15:24:58 +00:00
import shutil
@auth.before_app_request
def before_request():
"""
Checks if a user is unconfirmed when visiting specific sites. Redirects to
unconfirmed view if user is unconfirmed.
"""
if current_user.is_authenticated:
current_user.ping()
if not current_user.confirmed \
and request.endpoint \
and request.blueprint != 'auth' \
and request.endpoint != 'static':
return redirect(url_for('auth.unconfirmed'))
2020-02-19 15:24:58 +00:00
2020-02-19 15:36:55 +00:00
@auth.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-02-19 15:24:58 +00:00
login_form = LoginForm(prefix='login-form')
if login_form.validate_on_submit():
2020-02-20 08:45:38 +00:00
user = User.query.filter_by(username=login_form.user.data).first()
if user is None:
user = User.query.filter_by(email=login_form.user.data).first()
2020-02-19 15:24:58 +00:00
if user is not None and user.verify_password(login_form.password.data):
login_user(user, login_form.remember_me.data)
next = request.args.get('next')
if next is None or not next.startswith('/'):
next = url_for('main.dashboard')
return redirect(next)
2020-02-20 08:45:38 +00:00
flash('Invalid email/username or password.')
2020-02-19 15:24:58 +00:00
return render_template('auth/login.html.j2', login_form=login_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'))
@auth.route('/register', methods=['GET', 'POST'])
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-02-17 15:14:24 +00:00
registration_form = RegistrationForm(prefix='registration-form')
2019-11-14 12:40:05 +00:00
if registration_form.validate_on_submit():
user = User(email=registration_form.email.data.lower(),
password=registration_form.password.data,
username=registration_form.username.data)
2019-07-08 09:27:54 +00:00
db.session.add(user)
db.session.commit()
user_dir = os.path.join(current_app.config['DATA_DIR'],
2020-02-19 15:24:58 +00:00
str(user.id))
if os.path.exists(user_dir):
shutil.rmtree(user_dir)
os.mkdir(user_dir)
token = user.generate_confirmation_token()
2020-04-21 08:26:44 +00:00
msg = create_message(user.email, 'Confirm Your Account',
'auth/email/confirm', token=token, user=user)
send(msg)
2020-02-19 15:24:58 +00:00
flash('A confirmation email has been sent to you by email.')
return redirect(url_for('auth.login'))
2020-02-17 15:14:24 +00:00
return render_template('auth/register.html.j2',
registration_form=registration_form,
2019-09-23 14:39:36 +00:00
title='Register')
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:
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()
flash('You have confirmed your account. Thanks!')
2020-02-19 15:24:58 +00:00
return redirect(url_for('main.dashboard'))
2019-07-08 13:58:46 +00:00
else:
flash('The confirmation link is invalid or has expired.')
return redirect(url_for('auth.unconfirmed'))
@auth.route('/unconfirmed')
def unconfirmed():
2020-02-19 15:24:58 +00:00
if current_user.is_anonymous:
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')
@auth.route('/confirm')
@login_required
def resend_confirmation():
token = current_user.generate_confirmation_token()
2020-04-21 08:26:44 +00:00
msg = create_message(current_user.email, 'Confirm Your Account',
'auth/email/confirm', token=token, user=current_user)
send(msg)
2020-03-16 10:21:58 +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-08 09:04:52 +00:00
@auth.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-02-19 15:58:56 +00:00
reset_password_request_form = ResetPasswordRequestForm(
2020-04-21 08:26:44 +00:00
prefix='reset-password-request-form')
2020-02-19 15:24:58 +00:00
if reset_password_request_form.validate_on_submit():
submitted_email = reset_password_request_form.email.data
user = User.query.filter_by(email=submitted_email.lower()).first()
2019-07-08 09:04:52 +00:00
if user:
token = user.generate_reset_token()
2020-04-21 08:26:44 +00:00
msg = create_message(user.email, 'Reset Your Password',
'auth/email/reset_password', token=token,
user=user)
send(msg)
2019-07-08 09:04:52 +00:00
flash('An email with instructions to reset your password has been '
'sent to you.')
2020-02-19 15:24:58 +00:00
return redirect(url_for('auth.login'))
return render_template(
'auth/reset_password_request.html.j2',
reset_password_request_form=reset_password_request_form,
2020-04-21 08:26:44 +00:00
title='Password Reset')
2019-07-08 11:56:09 +00:00
2019-07-08 13:13:32 +00:00
@auth.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-02-19 15:58:56 +00:00
reset_password_form = ResetPasswordForm(prefix='reset-password-form')
2020-02-19 15:24:58 +00:00
if reset_password_form.validate_on_submit():
if User.reset_password(token, reset_password_form.password.data):
2019-07-08 13:13:32 +00:00
db.session.commit()
flash('Your password has been updated.')
2020-02-19 15:24:58 +00:00
return redirect(url_for('auth.login'))
2019-07-08 13:13:32 +00:00
else:
return redirect(url_for('main.index'))
2019-09-23 14:39:36 +00:00
return render_template('auth/reset_password.html.j2',
2020-02-19 15:24:58 +00:00
reset_password_form=reset_password_form,
2020-11-02 09:01:02 +00:00
title='Password Reset',
token=token)