Update app.py
Browse files
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 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
st.
|
| 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 |
-
st.
|
| 78 |
-
else:
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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()
|