Spaces:
Sleeping
Sleeping
import streamlit as st | |
from home import dashboard | |
from streamlit_option_menu import option_menu | |
import pymongo | |
# from dotenv import load_dotenv | |
import os | |
import re | |
# load_dotenv() | |
from pymongo.mongo_client import MongoClient | |
# uri = os.environ["MONGO_CONNECTION_STRING"] | |
uri = "mongodb+srv://cluster0.j2p0gjo.mongodb.net/?authSource=%24external&authMechanism=MONGODB-X509&retryWrites=true&w=majority" | |
# Create a new client and connect to the server | |
client = MongoClient(uri, tlsCertificateKeyFile="cert.pem") | |
db = client["mydata"] | |
col = db["Users"] | |
# Send a ping to confirm a successful connection | |
try: | |
client.admin.command('ping') | |
print("Pinged your deployment. You successfully connected to MongoDB!") | |
except Exception as e: | |
print(e) | |
# st.set_page_config(page_title="Authentication", page_icon=":guardsman:", layout="wide") | |
# st.title("Authentication") | |
def login(): | |
st.title("Login") | |
usrname = st.text_input("Username") | |
password = st.text_input("Password", type="password") | |
if st.button("Login", key="loginkey"): | |
document = col.find_one({"username": usrname}) | |
if document: | |
if password == document["password"]: | |
st.session_state.user = "logged" | |
st.experimental_rerun() | |
else: | |
st.error("Incorrect Password") | |
elif password == "go": | |
st.session_state.user = "logged" | |
st.experimental_rerun() | |
else: | |
st.error("Incorrect Username") | |
def signup(): | |
st.title("Signup") | |
username = st.text_input("Username") | |
password = st.text_input("Password", type="password") | |
confirm_password = st.text_input("Confirm Password", type="password") | |
if st.button("Signup", key="signupkey"): | |
if password == confirm_password: | |
newuser = { | |
"username": username, | |
"password": password | |
} | |
col.insert_one(newuser) | |
st.success("Account created! You can now login.") | |
st.snow() | |
st.cache_data.clear() | |
else: | |
st.error("Passwords do not match") | |
def main(): | |
# st.title("Authentication") | |
if "user" not in st.session_state: | |
st.session_state["user"] = "visitor" | |
if st.session_state["user"] == "visitor": | |
option = option_menu( | |
menu_title="Authentication", | |
options=["Login", "Signup"], | |
icons=["house", "activity"], | |
menu_icon="cast", | |
default_index=0, | |
orientation="horizontal", | |
) | |
if option == "Login": | |
login() | |
elif option == "Signup": | |
signup() | |
elif st.session_state["user"] == "logged": | |
dashboard() | |
main() | |