URIQ0023 commited on
Commit
84a23b7
·
verified ·
1 Parent(s): 9a0ac85

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +106 -81
app.py CHANGED
@@ -1,81 +1,106 @@
1
- import streamlit as st
2
- import pymongo
3
- import bcrypt
4
- import os
5
- import json
6
- from dotenv import load_dotenv
7
- import streamlit_authenticator as stauth
8
-
9
- # Load environment variables
10
- load_dotenv()
11
-
12
- # Use Hugging Face secrets if available
13
- if os.path.exists("secrets.json"):
14
- with open("secrets.json") as f:
15
- secrets = json.load(f)
16
- os.environ.update(secrets)
17
-
18
- MONGO_URI = os.getenv("MONGO_URI")
19
- SECRET_KEY = os.getenv("SECRET_KEY")
20
-
21
- if not MONGO_URI or not SECRET_KEY:
22
- st.error("Missing environment variables! Please check your Hugging Face Secrets.")
23
- st.stop()
24
-
25
- # Connect to MongoDB
26
- client = pymongo.MongoClient(MONGO_URI)
27
- db = client["user_database"]
28
- users_collection = db["users"]
29
-
30
- # Function to hash passwords
31
- def hash_password(password):
32
- return bcrypt.hashpw(password.encode("utf-8"), bcrypt.gensalt()).decode("utf-8")
33
-
34
- # Function to verify passwords
35
- def verify_password(password, hashed):
36
- return bcrypt.checkpw(password.encode("utf-8"), hashed.encode("utf-8"))
37
-
38
- # Streamlit Authentication Setup
39
- authenticator = stauth.Authenticate(
40
- credentials={},
41
- cookie_name="auth_cookie",
42
- key=SECRET_KEY,
43
- cookie_expiry_days=30
44
- )
45
-
46
- # UI Layout
47
- st.title("Secure Streamlit App with MongoDB Authentication")
48
-
49
- menu = st.sidebar.radio("Menu", ["Login", "Signup"])
50
-
51
- # Login Form
52
- if menu == "Login":
53
- st.subheader("Login")
54
-
55
- username = st.text_input("Username")
56
- password = st.text_input("Password", type="password")
57
-
58
- if st.button("Login"):
59
- user = users_collection.find_one({"username": username})
60
- if user and verify_password(password, user["password"]):
61
- st.success(f"Welcome {username}!")
62
- else:
63
- st.error("Invalid credentials. Please try again.")
64
-
65
- # Signup Form
66
- elif menu == "Signup":
67
- st.subheader("Sign Up")
68
-
69
- new_username = st.text_input("Choose a Username")
70
- new_password = st.text_input("Choose a Password", type="password")
71
- confirm_password = st.text_input("Confirm Password", type="password")
72
-
73
- if st.button("Sign Up"):
74
- if new_password != confirm_password:
75
- st.error("Passwords do not match.")
76
- elif users_collection.find_one({"username": new_username}):
77
- st.error("Username already exists.")
78
- else:
79
- hashed_password = hash_password(new_password)
80
- users_collection.insert_one({"username": new_username, "password": hashed_password})
81
- st.success("Account created successfully! You can now log in.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pymongo
3
+ import bcrypt
4
+ import os
5
+ import json
6
+ from dotenv import load_dotenv
7
+ import streamlit_authenticator as stauth
8
+
9
+ # Load environment variables
10
+ load_dotenv()
11
+
12
+ if os.path.exists("secrets.json"):
13
+ with open("secrets.json") as f:
14
+ secrets = json.load(f)
15
+ os.environ.update(secrets)
16
+
17
+ MONGO_URI = os.getenv("MONGO_URI")
18
+ SECRET_KEY = os.getenv("SECRET_KEY")
19
+
20
+ if not MONGO_URI or not SECRET_KEY:
21
+ st.error("Missing environment variables! Please check your Hugging Face Secrets.")
22
+ st.stop()
23
+
24
+ # Connect to MongoDB
25
+ client = pymongo.MongoClient(MONGO_URI)
26
+ db = client["user_database"]
27
+ users_collection = db["users"]
28
+
29
+ # Function to hash passwords
30
+ def hash_password(password):
31
+ return bcrypt.hashpw(password.encode("utf-8"), bcrypt.gensalt()).decode("utf-8")
32
+
33
+ # Function to verify passwords
34
+ def verify_password(password, hashed):
35
+ return bcrypt.checkpw(password.encode("utf-8"), hashed.encode("utf-8"))
36
+
37
+ # UI Customization
38
+ st.set_page_config(page_title="Secure Login App", page_icon="🔒", layout="centered")
39
+
40
+ # Light/Dark Mode Toggle
41
+ theme_mode = st.toggle("🌞 Light / 🌙 Dark Mode", value=True)
42
+
43
+ if theme_mode:
44
+ st.markdown(
45
+ """
46
+ <style>
47
+ body { background-color: #f0f2f6; color: black; }
48
+ .stTextInput>div>div>input, .stButton>button { background-color: white; }
49
+ </style>
50
+ """,
51
+ unsafe_allow_html=True
52
+ )
53
+ else:
54
+ st.markdown(
55
+ """
56
+ <style>
57
+ body { background-color: #1e1e1e; color: white; }
58
+ .stTextInput>div>div>input, .stButton>button { background-color: #333; color: white; }
59
+ </style>
60
+ """,
61
+ unsafe_allow_html=True
62
+ )
63
+
64
+ # Login and Signup Flow
65
+ if "signup_mode" not in st.session_state:
66
+ st.session_state.signup_mode = False
67
+
68
+ if not st.session_state.signup_mode:
69
+ st.subheader("🔑 Login to Your Account")
70
+
71
+ username = st.text_input("Username")
72
+ password = st.text_input("Password", type="password")
73
+
74
+ if st.button("Login"):
75
+ user = users_collection.find_one({"username": username})
76
+ if user and verify_password(password, user["password"]):
77
+ st.success(f"Welcome {username}!")
78
+ else:
79
+ st.error("Invalid credentials. Please try again.")
80
+
81
+ # Hyperlink to switch to signup
82
+ if st.button("Sign up instead"):
83
+ st.session_state.signup_mode = True
84
+ st.rerun()
85
+
86
+ else:
87
+ st.subheader("📝 Create a New Account")
88
+
89
+ new_username = st.text_input("Choose a Username")
90
+ new_password = st.text_input("Choose a Password", type="password")
91
+ confirm_password = st.text_input("Confirm Password", type="password")
92
+
93
+ if st.button("Sign Up"):
94
+ if new_password != confirm_password:
95
+ st.error("Passwords do not match.")
96
+ elif users_collection.find_one({"username": new_username}):
97
+ st.error("Username already exists.")
98
+ else:
99
+ hashed_password = hash_password(new_password)
100
+ users_collection.insert_one({"username": new_username, "password": hashed_password})
101
+ st.success("Account created successfully! You can now log in.")
102
+
103
+ # Hyperlink to switch back to login
104
+ if st.button("Back to Login"):
105
+ st.session_state.signup_mode = False
106
+ st.rerun()