130 lines
5.0 KiB
Python
130 lines
5.0 KiB
Python
import datetime
|
|
from flask import render_template, redirect, request, url_for, flash
|
|
from flask_login import login_user, logout_user, login_required, \
|
|
current_user
|
|
from . import auth
|
|
from .. import db
|
|
from ..models import User, Role, Provider
|
|
from ..email import send_email
|
|
from .forms import LoginForm, RegistrationForm, PasswordResetRequestForm, \
|
|
PasswordResetForm
|
|
|
|
@auth.before_app_request
|
|
def before_request():
|
|
if current_user.is_authenticated \
|
|
and not current_user.confirmed \
|
|
and request.endpoint[:5] != 'auth.' \
|
|
and '/static/' not in request.path:
|
|
return redirect(url_for('auth.unconfirmed'))
|
|
if current_user.is_authenticated \
|
|
and current_user.suspended \
|
|
and request.endpoint[:5] != 'auth.' \
|
|
and '/static/' not in request.path:
|
|
return redirect(url_for('auth.suspended'))
|
|
|
|
@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')
|
|
|
|
@auth.route('/suspended')
|
|
def suspended():
|
|
if current_user.is_anonymous or not current_user.suspended:
|
|
return redirect(url_for('main.index'))
|
|
return render_template('auth/suspended.html')
|
|
|
|
@auth.route('/login', methods=['GET', 'POST'])
|
|
def login():
|
|
form = LoginForm()
|
|
if form.validate_on_submit():
|
|
user = User.query.filter_by(email=form.email.data).first()
|
|
if user is not None and user.verify_password(form.password.data):
|
|
login_user(user, form.remember_me.data)
|
|
return redirect(request.args.get('next') or url_for('main.index'))
|
|
flash('Invalid username or password.')
|
|
return render_template('auth/login.html', form=form)
|
|
|
|
@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():
|
|
form = RegistrationForm()
|
|
if form.validate_on_submit():
|
|
r = Role.query.filter_by(default=True).first()
|
|
user = User(email=form.email.data,
|
|
username=form.username.data,
|
|
full_name=form.full_name.data,
|
|
password=form.password.data,
|
|
avatar="/static/img/user2-160x160.jpg",
|
|
created_at=datetime.datetime.now(),
|
|
role_id=r.id)
|
|
db.session.add(user)
|
|
db.session.commit()
|
|
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.')
|
|
return redirect(url_for('main.index'))
|
|
return render_template('auth/register.html', form=form)
|
|
|
|
# user email confirmation function
|
|
@auth.route('/confirm/<token>')
|
|
@login_required
|
|
def confirm(token):
|
|
if current_user.confirmed:
|
|
return redirect(url_for('main.index'))
|
|
if current_user.confirm(token):
|
|
flash('You have confirmed your account. Thanks!')
|
|
else:
|
|
flash('The confirmation link is invalid or has expired.')
|
|
return redirect(url_for('main.index'))
|
|
|
|
# resend confirmation email
|
|
@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 been sent to you by email.')
|
|
return redirect(url_for('main.index'))
|
|
|
|
# password reset
|
|
@auth.route('/reset/<token>', methods=['GET', 'POST'])
|
|
def password_reset(token):
|
|
if not current_user.is_anonymous:
|
|
return redirect(url_for('main.index'))
|
|
form = PasswordResetForm()
|
|
if form.validate_on_submit():
|
|
user = User.query.filter_by(email=form.email.data).first()
|
|
if user is None:
|
|
return redirect(url_for('main.index'))
|
|
if user.reset_password(token, form.password.data):
|
|
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', form=form)
|
|
|
|
@auth.route('/reset', methods=['GET', 'POST'])
|
|
def password_reset_request():
|
|
if not current_user.is_anonymous:
|
|
return redirect(url_for('main.index'))
|
|
form = PasswordResetRequestForm()
|
|
if form.validate_on_submit():
|
|
user = User.query.filter_by(email=form.email.data).first()
|
|
if user:
|
|
token = user.generate_reset_token()
|
|
send_email(user.email, 'Reset Your Password',
|
|
'auth/email/reset_password',
|
|
user=user, token=token,
|
|
next=request.args.get('next'))
|
|
flash('Instruction to reset your password has been '
|
|
'send to your email address.')
|
|
return render_template('auth/password_reset_request.html', form=form) |