2025-10-16 15:14:51 +02:00
|
|
|
"""Authentication routes"""
|
2025-10-17 13:32:09 +02:00
|
|
|
from flask import Blueprint, render_template, request, redirect, url_for, flash, current_app
|
|
|
|
|
from flask_login import login_user, logout_user, login_required, current_user
|
2025-10-16 15:14:51 +02:00
|
|
|
from app.models.user import User
|
|
|
|
|
|
|
|
|
|
|
2025-10-16 20:11:16 +02:00
|
|
|
bp = Blueprint('auth', __name__)
|
2025-10-16 15:14:51 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@bp.route('/login', methods=['GET', 'POST'])
|
|
|
|
|
def login():
|
|
|
|
|
"""User login page
|
|
|
|
|
|
|
|
|
|
GET: Display login form
|
|
|
|
|
POST: Process login credentials
|
|
|
|
|
"""
|
|
|
|
|
if request.method == 'POST':
|
|
|
|
|
username = request.form.get('username', '').strip()
|
|
|
|
|
password = request.form.get('password', '')
|
|
|
|
|
|
|
|
|
|
if not username or not password:
|
2025-10-17 13:32:09 +02:00
|
|
|
current_app.logger.warning('Login attempt with missing credentials')
|
2025-10-16 15:14:51 +02:00
|
|
|
flash('Please provide both username and password', 'error')
|
|
|
|
|
return render_template('auth/login.html')
|
|
|
|
|
|
|
|
|
|
user = User.get_by_username(username)
|
|
|
|
|
|
|
|
|
|
if user and user.is_active and user.check_password(password):
|
|
|
|
|
login_user(user)
|
2025-10-17 13:32:09 +02:00
|
|
|
current_app.logger.info(f'User logged in successfully: {username} (role: {user.role})')
|
2025-10-16 15:14:51 +02:00
|
|
|
flash(f'Welcome back, {user.username}!', 'success')
|
|
|
|
|
|
2025-10-16 20:11:16 +02:00
|
|
|
# Redirect to dashboard for product owners and administrators
|
|
|
|
|
return redirect(url_for('dashboard.list'))
|
2025-10-16 15:14:51 +02:00
|
|
|
else:
|
2025-10-17 13:32:09 +02:00
|
|
|
current_app.logger.warning(f'Failed login attempt for username: {username}')
|
2025-10-16 15:14:51 +02:00
|
|
|
flash('Invalid username or password', 'error')
|
|
|
|
|
|
|
|
|
|
return render_template('auth/login.html')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@bp.route('/logout')
|
|
|
|
|
@login_required
|
|
|
|
|
def logout():
|
|
|
|
|
"""User logout"""
|
2025-10-17 13:32:09 +02:00
|
|
|
username = current_user.username
|
2025-10-16 15:14:51 +02:00
|
|
|
logout_user()
|
2025-10-17 13:32:09 +02:00
|
|
|
current_app.logger.info(f'User logged out: {username}')
|
2025-10-16 15:14:51 +02:00
|
|
|
flash('You have been logged out', 'info')
|
2025-10-17 15:26:54 +02:00
|
|
|
return redirect(url_for('landing.index'))
|