Bug fixing fehlende oder Fehlerhafte Module korrigiert
This commit is contained in:
@@ -1,9 +1,13 @@
|
||||
from flask import Blueprint
|
||||
from auth.login import login_route
|
||||
from auth.logout import logout_route
|
||||
from auth.setup_admin import initialize_admin_user
|
||||
|
||||
auth_bp = Blueprint('auth', __name__)
|
||||
|
||||
# Endpunkte registrieren
|
||||
auth_bp.add_url_rule('/api/login', view_func=login_route, methods=['POST'])
|
||||
auth_bp.add_url_rule('/api/logout', view_func=logout_route, methods=['POST'])
|
||||
|
||||
# exportiere Blueprint und Admin-Setup
|
||||
__all__ = ['auth_bp', 'initialize_admin_user']
|
||||
|
||||
@@ -5,7 +5,7 @@ from datetime import datetime, timedelta
|
||||
import jwt
|
||||
|
||||
from util.logger import logger
|
||||
from util.db_config import load_db_config
|
||||
from util.db_config import load_config
|
||||
from auth.token import SECRET_KEY
|
||||
|
||||
def login_route():
|
||||
@@ -14,7 +14,7 @@ def login_route():
|
||||
password = data.get('password')
|
||||
|
||||
try:
|
||||
config = load_db_config()
|
||||
config = load_config()
|
||||
conn = connect(**config)
|
||||
cursor = conn.cursor(dictionary=True)
|
||||
cursor.execute("SELECT * FROM users WHERE username = %s", (username,))
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
# auth/setup_admin.py
|
||||
|
||||
import mysql.connector
|
||||
from werkzeug.security import generate_password_hash
|
||||
from util.logger import logger
|
||||
|
||||
def initialize_admin_user(config):
|
||||
try:
|
||||
conn = mysql.connector.connect(**config)
|
||||
cursor = conn.cursor()
|
||||
|
||||
cursor.execute("""
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
username VARCHAR(50) NOT NULL UNIQUE,
|
||||
password VARCHAR(255) NOT NULL,
|
||||
role VARCHAR(20) NOT NULL
|
||||
)
|
||||
""")
|
||||
|
||||
# 🔍 Prüfe, ob es bereits einen Admin gibt
|
||||
cursor.execute("SELECT COUNT(*) FROM users WHERE role = 'admin'")
|
||||
if cursor.fetchone()[0] == 0:
|
||||
cursor.execute(
|
||||
"INSERT INTO users (username, password, role) VALUES (%s, %s, %s)",
|
||||
("admin", generate_password_hash("admin"), "admin")
|
||||
)
|
||||
logger.info("Admin user created (username: admin, password: admin)")
|
||||
else:
|
||||
logger.info("Admin user already exists.")
|
||||
|
||||
conn.commit()
|
||||
cursor.close()
|
||||
conn.close()
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"[Admin-Setup] Failed to initialize admin user: {e}")
|
||||
Reference in New Issue
Block a user