fabianad commited on
Commit
1f43f19
·
verified ·
1 Parent(s): a74b40e

Create auth.py

Browse files
Files changed (1) hide show
  1. auth.py +131 -0
auth.py ADDED
@@ -0,0 +1,131 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import hashlib
3
+ import re
4
+ from models import User, SessionLocal
5
+
6
+ def validate_password(password: str) -> tuple[bool, str]:
7
+ """Validate password requirements."""
8
+ if len(password) < 8:
9
+ return False, "Password must be at least 8 characters long"
10
+ if not any(c.isupper() for c in password):
11
+ return False, "Password must contain at least one uppercase letter"
12
+ return True, ""
13
+
14
+ def hash_password(password: str) -> str:
15
+ """Hash a password for storing."""
16
+ return hashlib.sha256(password.encode()).hexdigest()
17
+
18
+ def verify_password(stored_password: str, provided_password: str) -> bool:
19
+ """Verify a stored password against one provided by user"""
20
+ return stored_password == hash_password(provided_password)
21
+
22
+ def login_user(username: str, password: str) -> bool:
23
+ """Verify user credentials and log them in."""
24
+ db = SessionLocal()
25
+ try:
26
+ user = db.query(User).filter(User.username == username).first()
27
+ if user and verify_password(user.password, password):
28
+ st.session_state.user_id = user.id
29
+ st.session_state.username = user.username
30
+ return True
31
+ return False
32
+ finally:
33
+ db.close()
34
+
35
+ def signup_user(username: str, password: str) -> tuple[bool, str]:
36
+ """Create a new user account."""
37
+ # Validate password
38
+ is_valid, message = validate_password(password)
39
+ if not is_valid:
40
+ return False, message
41
+
42
+ db = SessionLocal()
43
+ try:
44
+ # Check if username already exists
45
+ if db.query(User).filter(User.username == username).first():
46
+ return False, "Username already exists"
47
+
48
+ # Create new user
49
+ user = User(
50
+ username=username,
51
+ password=hash_password(password)
52
+ )
53
+ db.add(user)
54
+ db.commit()
55
+
56
+ # Log in the new user
57
+ st.session_state.user_id = user.id
58
+ st.session_state.username = user.username
59
+ return True, "Account created successfully"
60
+ except Exception as e:
61
+ db.rollback()
62
+ return False, str(e)
63
+ finally:
64
+ db.close()
65
+
66
+ def update_profile(user_id: int, **profile_data) -> tuple[bool, str]:
67
+ """Update user profile information."""
68
+ db = SessionLocal()
69
+ try:
70
+ user = db.query(User).filter(User.id == user_id).first()
71
+ if not user:
72
+ return False, "User not found"
73
+
74
+ # Update user fields
75
+ for field, value in profile_data.items():
76
+ if hasattr(user, field):
77
+ setattr(user, field, value)
78
+
79
+ db.commit()
80
+ return True, "Profile updated successfully"
81
+ except Exception as e:
82
+ db.rollback()
83
+ return False, str(e)
84
+ finally:
85
+ db.close()
86
+
87
+ def get_user_profile(user_id: int) -> User:
88
+ """Get user profile information."""
89
+ db = SessionLocal()
90
+ try:
91
+ return db.query(User).filter(User.id == user_id).first()
92
+ finally:
93
+ db.close()
94
+
95
+ def change_password(user_id: int, current_password: str, new_password: str) -> tuple[bool, str]:
96
+ """Change user password."""
97
+ # Validate new password
98
+ is_valid, message = validate_password(new_password)
99
+ if not is_valid:
100
+ return False, message
101
+
102
+ db = SessionLocal()
103
+ try:
104
+ user = db.query(User).filter(User.id == user_id).first()
105
+ if not user:
106
+ return False, "User not found"
107
+
108
+ # Verify current password
109
+ if not verify_password(user.password, current_password):
110
+ return False, "Current password is incorrect"
111
+
112
+ # Update password
113
+ user.password = hash_password(new_password)
114
+ db.commit()
115
+ return True, "Password updated successfully"
116
+ except Exception as e:
117
+ db.rollback()
118
+ return False, str(e)
119
+ finally:
120
+ db.close()
121
+
122
+ def is_logged_in() -> bool:
123
+ """Check if user is logged in."""
124
+ return 'user_id' in st.session_state
125
+
126
+ def logout_user():
127
+ """Log out the current user."""
128
+ if 'user_id' in st.session_state:
129
+ del st.session_state.user_id
130
+ if 'username' in st.session_state:
131
+ del st.session_state.username