147 lines
5.4 KiB
Python
147 lines
5.4 KiB
Python
from flask import render_template, redirect, url_for, flash, request, current_app, jsonify, session
|
|
from flask_login import login_user, logout_user, login_required, current_user
|
|
from urllib.parse import urlparse
|
|
from app import db
|
|
from app.models import User
|
|
from app.routes import auth_bp
|
|
from flask_wtf import FlaskForm
|
|
from wtforms import StringField, PasswordField, BooleanField, SubmitField
|
|
from wtforms.validators import DataRequired, Length, EqualTo, ValidationError
|
|
from werkzeug.exceptions import BadRequest
|
|
|
|
# Login form
|
|
class LoginForm(FlaskForm):
|
|
username = StringField('Username', validators=[DataRequired()])
|
|
password = PasswordField('Password', validators=[DataRequired()])
|
|
remember_me = BooleanField('Remember Me')
|
|
submit = SubmitField('Sign In')
|
|
|
|
# Registration form
|
|
class RegistrationForm(FlaskForm):
|
|
username = StringField('Username', validators=[DataRequired(), Length(min=3, max=64)])
|
|
password = PasswordField('Password', validators=[DataRequired(), Length(min=8)])
|
|
password2 = PasswordField('Confirm 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.')
|
|
|
|
@auth_bp.route('/login', methods=['GET', 'POST'])
|
|
def login():
|
|
if current_user.is_authenticated:
|
|
return redirect(url_for('dashboard.index'))
|
|
|
|
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', 'error')
|
|
return redirect(url_for('auth.login'))
|
|
|
|
login_user(user, remember=form.remember_me.data)
|
|
|
|
next_page = request.args.get('next')
|
|
if not next_page or urlparse(next_page).netloc != '':
|
|
next_page = url_for('dashboard.index')
|
|
|
|
return redirect(next_page)
|
|
|
|
return render_template('auth/login.html', title='Sign In', 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'))
|
|
|
|
@auth_bp.route('/register', methods=['GET', 'POST'])
|
|
def register():
|
|
if current_user.is_authenticated:
|
|
return redirect(url_for('dashboard.index'))
|
|
|
|
form = RegistrationForm()
|
|
if form.validate_on_submit():
|
|
user = User(username=form.username.data)
|
|
user.set_password(form.password.data)
|
|
db.session.add(user)
|
|
db.session.commit()
|
|
flash('Registration successful! You can now log in.', 'success')
|
|
return redirect(url_for('auth.login'))
|
|
|
|
return render_template('auth/register.html', title='Register', form=form)
|
|
|
|
@auth_bp.route('/update_profile', methods=['POST'])
|
|
@login_required
|
|
def update_profile():
|
|
"""Update user profile information"""
|
|
username = request.form.get('username')
|
|
|
|
if not username or username.strip() == '':
|
|
flash('Username cannot be empty', 'error')
|
|
return redirect(url_for('auth.profile'))
|
|
|
|
# Check if username is already taken by another user
|
|
existing_user = User.query.filter(User.username == username, User.id != current_user.id).first()
|
|
if existing_user:
|
|
flash('Username is already taken', 'error')
|
|
return redirect(url_for('auth.profile'))
|
|
|
|
# Update username
|
|
current_user.username = username
|
|
db.session.commit()
|
|
flash('Profile updated successfully', 'success')
|
|
return redirect(url_for('auth.profile'))
|
|
|
|
@auth_bp.route('/change_password', methods=['POST'])
|
|
@login_required
|
|
def change_password():
|
|
"""Change user password"""
|
|
current_password = request.form.get('current_password')
|
|
new_password = request.form.get('new_password')
|
|
confirm_password = request.form.get('confirm_password')
|
|
|
|
# Validate input
|
|
if not all([current_password, new_password, confirm_password]):
|
|
flash('All fields are required', 'error')
|
|
return redirect(url_for('auth.profile'))
|
|
|
|
if new_password != confirm_password:
|
|
flash('New passwords do not match', 'error')
|
|
return redirect(url_for('auth.profile'))
|
|
|
|
# Check current password
|
|
if not current_user.check_password(current_password):
|
|
flash('Current password is incorrect', 'error')
|
|
return redirect(url_for('auth.profile'))
|
|
|
|
# Set new password
|
|
current_user.set_password(new_password)
|
|
db.session.commit()
|
|
flash('Password changed successfully', 'success')
|
|
return redirect(url_for('auth.profile'))
|
|
|
|
@auth_bp.route('/update_preferences', methods=['POST'])
|
|
@login_required
|
|
def update_preferences():
|
|
"""Update user preferences like theme"""
|
|
theme_preference = request.form.get('theme_preference', 'system')
|
|
|
|
# Store in session for now, but could be added to user model
|
|
session['theme_preference'] = theme_preference
|
|
|
|
flash('Preferences updated successfully', 'success')
|
|
return redirect(url_for('auth.profile'))
|
|
|
|
@auth_bp.route('/profile')
|
|
@login_required
|
|
def profile():
|
|
# Get theme preference from session or default to system
|
|
theme_preference = session.get('theme_preference', 'system')
|
|
|
|
return render_template('auth/profile.html',
|
|
title='User Profile',
|
|
theme_preference=theme_preference)
|