Repository navigation
Expand file tree
/
Copy pathauth.py
More file actions
75 lines (57 loc) · 2.35 KB
/
Copy pathauth.py
File metadata and controls
75 lines (57 loc) · 2.35 KB
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
# auth.py
import streamlit as st
from utils import create_user, get_user_by_email, update_last_login, hash_password, load_user_listings, init_db
# Initialize DB at start
init_db()
def require_auth():
if "user" not in st.session_state:
st.session_state.user = None
if "page" not in st.session_state:
st.session_state.page = "Dashboard"
if st.session_state.user is None:
login_signup_ui()
def login_signup_ui():
st.markdown("## 🔐 Login / Sign Up")
option = st.radio("Select option", ["Login", "Sign Up"], horizontal=True)
name = st.text_input("Full Name") if option == "Sign Up" else ""
email = st.text_input("Email")
password = st.text_input("Password", type="password")
location = st.text_input("Location") if option == "Sign Up" else ""
if st.button(option, use_container_width=True):
# ============ VALIDATION ============
if not email or not password or (option == "Sign Up" and (not name or not location)):
st.error("Please fill all required fields")
return
# ============ SIGN UP ============
if option == "Sign Up":
success, msg = create_user(name, email, hash_password(password), location)
if not success:
st.error(msg)
return
st.session_state.user = {
"name": name,
"email": email,
"location": location
}
st.session_state.item_list = load_user_listings(email)
st.session_state.page = "Dashboard"
st.rerun()
# ============ LOGIN ============
else:
user = get_user_by_email(email)
if not user:
st.error("No user found with this email.")
return
if user[3] != hash_password(password):
st.error("Incorrect password.")
return
st.session_state.user = {
"name": user[1],
"email": user[2],
"location": user[4]
}
update_last_login(email)
st.session_state.item_list = load_user_listings(email)
st.success("Logged in successfully!")
st.session_state.page = "Dashboard"
st.rerun()