nopaque/app/auth/routes.py

185 lines
5.6 KiB
Python
Raw Normal View History

2023-03-13 15:22:42 +00:00
from flask import abort, 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
2022-09-02 11:07:30 +00:00
from app import db
from app.email import create_message, send
from app.models import User
from . import bp
from .forms import (
LoginForm,
ResetPasswordForm,
ResetPasswordRequestForm,
RegistrationForm
)
2020-02-19 15:24:58 +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.
"""
if current_user.is_authenticated:
2022-09-02 11:07:30 +00:00
current_user.ping()
db.session.commit()
2022-09-02 11:07:30 +00:00
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
@bp.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'))
form = RegistrationForm()
if form.validate_on_submit():
try:
2022-09-02 11:07:30 +00:00
user = User.create(
email=form.email.data.lower(),
password=form.password.data,
2023-04-13 14:08:07 +00:00
username=form.username.data,
terms_of_use_accepted=form.terms_of_use_accepted.data
2022-09-02 11:07:30 +00:00
)
except OSError:
flash('Internal Server Error', category='error')
abort(500)
2022-09-02 11:07:30 +00:00
flash(f'User "{user.username}" created')
token = user.generate_confirm_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'))
return render_template(
'auth/register.html.j2',
2023-03-28 12:19:37 +00:00
title='Register',
form=form
)
2019-07-08 09:04:52 +00:00
2022-09-02 11:07:30 +00:00
@bp.route('/login', methods=['GET', 'POST'])
def login():
if current_user.is_authenticated:
2020-02-19 15:24:58 +00:00
return redirect(url_for('main.dashboard'))
form = LoginForm()
2022-09-02 11:07:30 +00:00
if form.validate_on_submit():
user = User.query.filter((User.email == form.user.data.lower()) | (User.username == form.user.data)).first()
if user 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.dashboard')
flash('You have been logged in')
return redirect(next)
flash('Invalid email/username or password', category='error')
2023-03-28 12:19:37 +00:00
return render_template(
'auth/login.html.j2',
title='Log in',
form=form
)
2022-09-02 11:07:30 +00:00
@bp.route('/logout')
@login_required
def logout():
logout_user()
flash('You have been logged out')
return redirect(url_for('main.index'))
@bp.route('/unconfirmed')
2022-09-02 11:07:30 +00:00
@login_required
def unconfirmed():
2022-09-02 11:07:30 +00:00
if current_user.confirmed:
2020-02-19 15:24:58 +00:00
return redirect(url_for('main.dashboard'))
2023-03-28 12:19:37 +00:00
return render_template(
'auth/unconfirmed.html.j2',
title='Unconfirmed'
)
2023-03-14 10:13:35 +00:00
@bp.route('/confirm-request')
@login_required
2022-09-02 11:07:30 +00:00
def confirm_request():
if current_user.confirmed:
return redirect(url_for('main.dashboard'))
token = current_user.generate_confirm_token()
msg = create_message(
current_user.email,
'Confirm Your Account',
'auth/email/confirm',
token=token,
user=current_user
)
send(msg)
flash('A new confirmation email has been sent to you by email')
2022-09-02 11:07:30 +00:00
return redirect(url_for('.unconfirmed'))
2022-09-02 11:07:30 +00:00
@bp.route('/confirm/<token>')
@login_required
def confirm(token):
if current_user.confirmed:
return redirect(url_for('main.dashboard'))
if current_user.confirm(token):
db.session.commit()
flash('You have confirmed your account')
return redirect(url_for('main.dashboard'))
flash('The confirmation link is invalid or has expired', category='error')
return redirect(url_for('.unconfirmed'))
2023-03-14 10:13:35 +00:00
@bp.route('/reset-password-request', 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'))
form = ResetPasswordRequestForm()
if form.validate_on_submit():
user = User.query.filter_by(email=form.email.data.lower()).first()
if user is not None:
2022-09-02 11:07:30 +00:00
token = user.generate_reset_password_token()
msg = create_message(
user.email,
'Reset Your Password',
'auth/email/reset_password',
token=token,
user=user
)
send(msg)
flash(
2022-09-02 11:07:30 +00:00
'An email with instructions to reset your password has been sent '
'to you'
)
return redirect(url_for('.login'))
return render_template(
'auth/reset_password_request.html.j2',
2023-03-28 12:19:37 +00:00
title='Password Reset',
form=form
)
2019-07-08 11:56:09 +00:00
2023-03-14 10:13:35 +00:00
@bp.route('/reset-password/<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'))
form = ResetPasswordForm()
if form.validate_on_submit():
if User.reset_password(token, form.password.data):
2019-07-08 13:13:32 +00:00
db.session.commit()
flash('Your password has been updated')
return redirect(url_for('.login'))
2022-09-02 11:07:30 +00:00
return redirect(url_for('main.index'))
return render_template(
'auth/reset_password.html.j2',
title='Password Reset',
2023-03-28 12:19:37 +00:00
form=form,
token=token
)