Spaces:
Sleeping
Sleeping
import streamlit as st | |
from streamlit_option_menu import option_menu | |
from home import dashboard | |
import os | |
from pymongo import MongoClient | |
if 'user' not in st.session_state: | |
st.session_state['user'] = 'visitor' | |
uri = os.environ["MONGO_CONNECTION_STRING"] | |
client = MongoClient(uri, tlsCertificateKeyFile="cert.pem") | |
db = client["reshav"] | |
col=db["mine"] | |
try: | |
client.admin.command("ping") | |
print("Connection Established!") | |
except Exception as e: | |
print(e) | |
def login(): | |
st.title("Login") | |
data = json.load(open("database/data.json")) | |
usrname = st.text_input("Username") | |
password = st.text_input("Password", type="password") | |
if st.button("Login", key="loginkey"): | |
for user in data["users"]: | |
if usrname == user["username"] and password == user["password"]: | |
st.success("Logged in as {}".format(usrname)) | |
st.session_state["user"] = "logged" | |
flag = True | |
st.experimental_rerun() | |
else: | |
flag = False | |
if flag == False: | |
st.error("Invalid username or password") | |
st.stop() | |
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") | |
else: | |
st.error("Passwords do not match") | |
def main(): | |
if st.session_state["user"] == "visitor": | |
option = option_menu( | |
menu_title="Authentication", | |
options=["Login", "Signup"], | |
) | |
if option == "Login": | |
login() | |
elif option == "Signup": | |
signup() | |
elif st.session_state["user"] == "logged": | |
dashboard() | |
main() |