2019-11-05 15:22:36 +00:00
|
|
|
from app import db
|
|
|
|
from app.email import send_email
|
|
|
|
from app.models import User
|
2019-11-14 09:01:45 +00:00
|
|
|
from flask import (current_app, flash, redirect, render_template, request,
|
|
|
|
url_for)
|
2020-02-17 13:23:19 +00:00
|
|
|
from flask_login import current_user, login_required, logout_user
|
2019-07-04 13:17:51 +00:00
|
|
|
from . import auth
|
2020-02-17 13:23:19 +00:00
|
|
|
from .forms import (PasswordResetForm, PasswordResetRequestForm,
|
2019-09-23 14:11:01 +00:00
|
|
|
RegistrationForm)
|
2019-11-14 09:01:45 +00:00
|
|
|
import os
|
2019-07-04 13:17:51 +00:00
|
|
|
|
|
|
|
|
2019-07-05 12:47:35 +00:00
|
|
|
@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:
|
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()
|
2020-02-17 15:14:24 +00:00
|
|
|
dir = os.path.join(current_app.config['NOPAQUE_STORAGE'], str(user.id))
|
2019-11-14 09:01:45 +00:00
|
|
|
try:
|
|
|
|
os.makedirs(dir)
|
|
|
|
except OSError:
|
|
|
|
flash('[ERROR]!')
|
|
|
|
user.delete()
|
|
|
|
else:
|
|
|
|
token = user.generate_confirmation_token()
|
2019-11-14 12:40:05 +00:00
|
|
|
send_email(user.email, 'Confirm Your Account',
|
|
|
|
'auth/email/confirm', token=token, user=user)
|
2019-11-14 09:01:45 +00:00
|
|
|
flash('A confirmation email has been sent to you by email.')
|
2020-02-17 15:14:24 +00:00
|
|
|
return redirect(url_for('main.index'))
|
|
|
|
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:
|
|
|
|
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.')
|
2019-08-02 11:24:52 +00:00
|
|
|
return redirect(url_for('main.dashboard'))
|
2019-07-08 13:58:46 +00:00
|
|
|
|
|
|
|
|
2019-07-09 08:09:35 +00:00
|
|
|
@auth.before_app_request
|
|
|
|
def before_request():
|
2019-07-11 13:43:48 +00:00
|
|
|
"""
|
|
|
|
Checks if a user is unconfirmed when visiting specific sites. Redirects to
|
|
|
|
unconfirmed view if user is unconfirmed.
|
|
|
|
"""
|
2019-09-23 14:39:36 +00:00
|
|
|
if (current_user.is_authenticated
|
|
|
|
and not current_user.confirmed
|
|
|
|
and request.blueprint != 'auth'
|
|
|
|
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'))
|
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
|
|
|
|
|
|
|
|
|
|
|
@auth.route('/confirm')
|
|
|
|
@login_required
|
|
|
|
def resend_confirmation():
|
|
|
|
token = current_user.generate_confirmation_token()
|
2019-09-23 14:39:36 +00:00
|
|
|
send_email(current_user.email,
|
|
|
|
'Confirm Your Account',
|
|
|
|
'auth/email/confirm',
|
|
|
|
token=token,
|
|
|
|
user=current_user)
|
2019-07-09 08:09:35 +00:00
|
|
|
flash('A new confirmation email has benn sent to you by email.')
|
2019-08-02 11:24:52 +00:00
|
|
|
return redirect(url_for('main.dashboard'))
|
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:
|
2019-08-02 11:24:52 +00:00
|
|
|
return redirect(url_for('main.dashboard'))
|
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()
|
2019-09-23 14:39:36 +00:00
|
|
|
send_email(user.email,
|
|
|
|
'Reset Your Password',
|
2019-07-08 09:04:52 +00:00
|
|
|
'auth/email/reset_password',
|
2019-09-23 14:39:36 +00:00
|
|
|
token=token,
|
|
|
|
user=user)
|
2019-07-08 09:04:52 +00:00
|
|
|
flash('An email with instructions to reset your password has been '
|
|
|
|
'sent to you.')
|
2020-02-17 15:14:24 +00:00
|
|
|
return redirect(url_for('main.index'))
|
2019-09-23 14:39:36 +00:00
|
|
|
return render_template('auth/reset_password_request.html.j2',
|
|
|
|
form=form,
|
2019-07-08 11:56:09 +00:00
|
|
|
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:
|
2019-08-02 11:24:52 +00:00
|
|
|
return redirect(url_for('main.dashboard'))
|
2019-07-08 13:13:32 +00:00
|
|
|
form = PasswordResetForm()
|
|
|
|
if form.validate_on_submit():
|
|
|
|
if User.reset_password(token, form.password.data):
|
|
|
|
db.session.commit()
|
|
|
|
flash('Your password has been updated.')
|
2020-02-17 15:14:24 +00:00
|
|
|
return redirect(url_for('main.index'))
|
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',
|
|
|
|
form=form,
|
2019-07-08 13:13:32 +00:00
|
|
|
title='Password Reset')
|