File size: 4,105 Bytes
761e949
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import streamlit as st
import hashlib
import re
from models import User, SessionLocal

def validate_password(password: str) -> tuple[bool, str]:
    """Validate password requirements."""
    if len(password) < 8:
        return False, "Password must be at least 8 characters long"
    if not any(c.isupper() for c in password):
        return False, "Password must contain at least one uppercase letter"
    return True, ""

def hash_password(password: str) -> str:
    """Hash a password for storing."""
    return hashlib.sha256(password.encode()).hexdigest()

def verify_password(stored_password: str, provided_password: str) -> bool:
    """Verify a stored password against one provided by user"""
    return stored_password == hash_password(provided_password)

def login_user(username: str, password: str) -> bool:
    """Verify user credentials and log them in."""
    db = SessionLocal()
    try:
        user = db.query(User).filter(User.username == username).first()
        if user and verify_password(user.password, password):
            st.session_state.user_id = user.id
            st.session_state.username = user.username
            return True
        return False
    finally:
        db.close()

def signup_user(username: str, password: str) -> tuple[bool, str]:
    """Create a new user account."""
    # Validate password
    is_valid, message = validate_password(password)
    if not is_valid:
        return False, message

    db = SessionLocal()
    try:
        # Check if username already exists
        if db.query(User).filter(User.username == username).first():
            return False, "Username already exists"

        # Create new user
        user = User(
            username=username,
            password=hash_password(password)
        )
        db.add(user)
        db.commit()

        # Log in the new user
        st.session_state.user_id = user.id
        st.session_state.username = user.username
        return True, "Account created successfully"
    except Exception as e:
        db.rollback()
        return False, str(e)
    finally:
        db.close()

def update_profile(user_id: int, **profile_data) -> tuple[bool, str]:
    """Update user profile information."""
    db = SessionLocal()
    try:
        user = db.query(User).filter(User.id == user_id).first()
        if not user:
            return False, "User not found"

        # Update user fields
        for field, value in profile_data.items():
            if hasattr(user, field):
                setattr(user, field, value)

        db.commit()
        return True, "Profile updated successfully"
    except Exception as e:
        db.rollback()
        return False, str(e)
    finally:
        db.close()

def get_user_profile(user_id: int) -> User:
    """Get user profile information."""
    db = SessionLocal()
    try:
        return db.query(User).filter(User.id == user_id).first()
    finally:
        db.close()

def change_password(user_id: int, current_password: str, new_password: str) -> tuple[bool, str]:
    """Change user password."""
    # Validate new password
    is_valid, message = validate_password(new_password)
    if not is_valid:
        return False, message

    db = SessionLocal()
    try:
        user = db.query(User).filter(User.id == user_id).first()
        if not user:
            return False, "User not found"

        # Verify current password
        if not verify_password(user.password, current_password):
            return False, "Current password is incorrect"

        # Update password
        user.password = hash_password(new_password)
        db.commit()
        return True, "Password updated successfully"
    except Exception as e:
        db.rollback()
        return False, str(e)
    finally:
        db.close()

def is_logged_in() -> bool:
    """Check if user is logged in."""
    return 'user_id' in st.session_state

def logout_user():
    """Log out the current user."""
    if 'user_id' in st.session_state:
        del st.session_state.user_id
    if 'username' in st.session_state:
        del st.session_state.username