35 lines
1.1 KiB
Python
35 lines
1.1 KiB
Python
from flask import Blueprint, render_template, redirect, url_for, request, flash
|
|
from flask_login import login_user, logout_user, login_required, current_user
|
|
from app import db
|
|
from app.models import User
|
|
from werkzeug.security import generate_password_hash, check_password_hash
|
|
|
|
bp = Blueprint('auth', __name__)
|
|
|
|
@bp.route('/')
|
|
def index():
|
|
return redirect(url_for('auth.login'))
|
|
|
|
@bp.route('/login', methods=['GET', 'POST'])
|
|
def login():
|
|
if request.method == 'POST':
|
|
email = request.form['email']
|
|
password = request.form['password']
|
|
user = User.query.filter_by(email=email).first()
|
|
|
|
if user and check_password_hash(user.password_hash, password):
|
|
login_user(user)
|
|
if user.role == 'admin':
|
|
return redirect(url_for('admin.dashboard'))
|
|
else:
|
|
return redirect(url_for('owner.dashboard'))
|
|
else:
|
|
flash('Credenciales inválidas')
|
|
return render_template('login.html')
|
|
|
|
@bp.route('/logout')
|
|
@login_required
|
|
def logout():
|
|
logout_user()
|
|
return redirect(url_for('auth.login'))
|