43 lines
1.7 KiB
Python
43 lines
1.7 KiB
Python
|
|
from flask import Blueprint, render_template, request, redirect, url_for, flash
|
||
|
|
from flask_login import login_user, logout_user, login_required, current_user
|
||
|
|
from app.models import User, db
|
||
|
|
from app.utils.security import generate_password_hash
|
||
|
|
from werkzeug.security import check_password_hash
|
||
|
|
from flask_wtf import FlaskForm
|
||
|
|
from wtforms import StringField, PasswordField, SubmitField
|
||
|
|
from wtforms.validators import DataRequired, Email, Length
|
||
|
|
|
||
|
|
auth_bp = Blueprint('auth', __name__)
|
||
|
|
|
||
|
|
class LoginForm(FlaskForm):
|
||
|
|
username = StringField('Username', validators=[DataRequired()])
|
||
|
|
password = PasswordField('Password', validators=[DataRequired()])
|
||
|
|
submit = SubmitField('Login')
|
||
|
|
|
||
|
|
@auth_bp.route('/login', methods=['GET', 'POST'])
|
||
|
|
def login():
|
||
|
|
if current_user.is_authenticated:
|
||
|
|
return redirect(url_for('inspections.dashboard'))
|
||
|
|
|
||
|
|
form = LoginForm()
|
||
|
|
if form.validate_on_submit():
|
||
|
|
user = User.query.filter_by(username=form.username.data).first()
|
||
|
|
if user and user.check_password(form.password.data):
|
||
|
|
if user.is_active:
|
||
|
|
login_user(user)
|
||
|
|
flash('Logged in successfully.', 'success')
|
||
|
|
next_page = request.args.get('next')
|
||
|
|
return redirect(next_page) if next_page else redirect(url_for('inspections.dashboard'))
|
||
|
|
else:
|
||
|
|
flash('Account is deactivated.', 'error')
|
||
|
|
else:
|
||
|
|
flash('Invalid username or password.', 'error')
|
||
|
|
|
||
|
|
return render_template('login.html', form=form)
|
||
|
|
|
||
|
|
@auth_bp.route('/logout')
|
||
|
|
@login_required
|
||
|
|
def logout():
|
||
|
|
logout_user()
|
||
|
|
flash('You have been logged out.', 'info')
|
||
|
|
return redirect(url_for('auth.login'))
|