goalsbreakdown/auth.py
Yuyao Huang f3bffa40cd Initial commit: GoalsBreakDown web app
- Flask backend with TinyDB database
- Multi-user auth with bcrypt password hashing
- Goal CRUD with activation/deactivation and per-user limits
- Task CRUD with status tracking (todo/doing/pending/done)
- Focus rule: one doing task per goal
- Time picker-style scroll view with drag-and-drop reordering
- Admin panel for user management
- uv environment management
2026-05-08 12:41:19 +08:00

46 lines
1.2 KiB
Python

from functools import wraps
from flask import session, redirect, url_for, jsonify
import bcrypt
import database
def hash_password(password):
return bcrypt.hashpw(
password.encode("utf-8"),
bcrypt.gensalt()
).decode("utf-8")
def check_password(password, password_hash):
return bcrypt.checkpw(
password.encode("utf-8"),
password_hash.encode("utf-8")
)
def login_required(f):
@wraps(f)
def decorated_function(*args, **kwargs):
if "user_id" not in session:
return jsonify({"success": False, "message": "Not authenticated"}), 401
return f(*args, **kwargs)
return decorated_function
def admin_required(f):
@wraps(f)
def decorated_function(*args, **kwargs):
if "user_id" not in session:
return jsonify({"success": False, "message": "Not authenticated"}), 401
user = database.get_user_by_id(session["user_id"])
if not user or user.get("role") != "admin":
return jsonify({"success": False, "message": "Admin access required"}), 403
return f(*args, **kwargs)
return decorated_function
def get_current_user():
if "user_id" not in session:
return None
return database.get_user_by_id(session["user_id"])