|
|
from flask import Blueprint, render_template, redirect, url_for, flash |
|
|
from flask_login import login_user, logout_user, current_user |
|
|
from app import db |
|
|
from app.models import User |
|
|
from flask_wtf import FlaskForm |
|
|
from wtforms import StringField, PasswordField, BooleanField, SubmitField |
|
|
from wtforms.validators import DataRequired, Email, EqualTo, ValidationError |
|
|
|
|
|
bp = Blueprint('auth', __name__) |
|
|
|
|
|
|
|
|
class LoginForm(FlaskForm): |
|
|
username = StringField('Username', validators=[DataRequired()]) |
|
|
password = PasswordField('Password', validators=[DataRequired()]) |
|
|
remember_me = BooleanField('Remember Me') |
|
|
submit = SubmitField('Sign In') |
|
|
|
|
|
class RegistrationForm(FlaskForm): |
|
|
username = StringField('Username', validators=[DataRequired()]) |
|
|
email = StringField('Email', validators=[DataRequired(), Email()]) |
|
|
password = PasswordField('Password', validators=[DataRequired()]) |
|
|
password2 = PasswordField( |
|
|
'Repeat Password', validators=[DataRequired(), EqualTo('password')]) |
|
|
submit = SubmitField('Register') |
|
|
|
|
|
def validate_username(self, username): |
|
|
user = User.query.filter_by(username=username.data).first() |
|
|
if user is not None: |
|
|
raise ValidationError('Please use a different username.') |
|
|
|
|
|
def validate_email(self, email): |
|
|
user = User.query.filter_by(email=email.data).first() |
|
|
if user is not None: |
|
|
raise ValidationError('Please use a different email address.') |
|
|
|
|
|
|
|
|
|
|
|
@bp.route('/login', methods=['GET', 'POST']) |
|
|
def login(): |
|
|
if current_user.is_authenticated: |
|
|
return redirect(url_for('main.dashboard')) |
|
|
form = LoginForm() |
|
|
if form.validate_on_submit(): |
|
|
user = User.query.filter_by(username=form.username.data).first() |
|
|
if user is None or not user.check_password(form.password.data): |
|
|
flash('Invalid username or password') |
|
|
return redirect(url_for('auth.login')) |
|
|
login_user(user, remember=form.remember_me.data) |
|
|
if user.is_admin: |
|
|
return redirect(url_for('main.admin_dashboard')) |
|
|
return redirect(url_for('main.dashboard')) |
|
|
return render_template('auth/login.html', title='Sign In', form=form) |
|
|
|
|
|
@bp.route('/logout') |
|
|
def logout(): |
|
|
logout_user() |
|
|
return redirect(url_for('main.index')) |
|
|
|
|
|
@bp.route('/register', methods=['GET', 'POST']) |
|
|
def register(): |
|
|
if current_user.is_authenticated: |
|
|
return redirect(url_for('main.dashboard')) |
|
|
form = RegistrationForm() |
|
|
if form.validate_on_submit(): |
|
|
user = User(username=form.username.data, email=form.email.data) |
|
|
user.set_password(form.password.data) |
|
|
|
|
|
if User.query.count() == 0: |
|
|
user.is_admin = True |
|
|
flash('Congratulations, you are now the admin!') |
|
|
db.session.add(user) |
|
|
db.session.commit() |
|
|
flash('Congratulations, you are now a registered user!') |
|
|
return redirect(url_for('auth.login')) |
|
|
return render_template('auth/register.html', title='Register', form=form) |