Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,6 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from home import dashboard
|
| 3 |
from streamlit_option_menu import option_menu
|
| 4 |
-
import json
|
| 5 |
-
import uuid
|
| 6 |
import pymongo
|
| 7 |
from dotenv import load_dotenv
|
| 8 |
import os
|
|
@@ -16,9 +14,9 @@ from pymongo.mongo_client import MongoClient
|
|
| 16 |
uri = os.environ["MONGO_CONNECTION_STRING"]
|
| 17 |
|
| 18 |
# Create a new client and connect to the server
|
| 19 |
-
client = MongoClient(uri)
|
| 20 |
|
| 21 |
-
db = client["
|
| 22 |
|
| 23 |
col = db["Users"]
|
| 24 |
|
|
@@ -36,20 +34,6 @@ except Exception as e:
|
|
| 36 |
# st.title("Authentication")
|
| 37 |
|
| 38 |
|
| 39 |
-
def load_json():
|
| 40 |
-
with open("database/data.json") as file:
|
| 41 |
-
data = json.load(file)
|
| 42 |
-
return data
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
def save_json():
|
| 49 |
-
with open("database/data.json", "w") as file:
|
| 50 |
-
json.dump(data, file, indent=4)
|
| 51 |
-
|
| 52 |
-
|
| 53 |
|
| 54 |
def login():
|
| 55 |
st.title("Login")
|
|
@@ -57,7 +41,8 @@ def login():
|
|
| 57 |
usrname = st.text_input("Username")
|
| 58 |
password = st.text_input("Password", type="password")
|
| 59 |
if st.button("Login", key="loginkey"):
|
| 60 |
-
|
|
|
|
| 61 |
if usrname == user["username"] and password == user["password"]:
|
| 62 |
st.success("Logged in as {}".format(usrname))
|
| 63 |
st.session_state["user"] = "logged"
|
|
@@ -79,14 +64,11 @@ def signup():
|
|
| 79 |
confirm_password = st.text_input("Confirm Password", type="password")
|
| 80 |
if st.button("Signup", key="signupkey"):
|
| 81 |
if password == confirm_password:
|
| 82 |
-
data = json.load(open("database/data.json"))
|
| 83 |
newuser = {
|
| 84 |
"username": username,
|
| 85 |
-
"password": password
|
| 86 |
-
"id": str(uuid.uuid4())
|
| 87 |
}
|
| 88 |
-
|
| 89 |
-
json.dump(data, open("database/data.json", "w"), indent=4)
|
| 90 |
st.success("Account created! You can now login.")
|
| 91 |
st.snow()
|
| 92 |
st.cache_data.clear()
|
|
@@ -100,10 +82,7 @@ def main():
|
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
-
if st.session_state["user"] == "
|
| 104 |
-
dashboard()
|
| 105 |
-
|
| 106 |
-
elif st.session_state["user"] == "visitor":
|
| 107 |
|
| 108 |
option = option_menu(
|
| 109 |
menu_title="Authentication",
|
|
@@ -118,7 +97,10 @@ def main():
|
|
| 118 |
login()
|
| 119 |
elif option == "Signup":
|
| 120 |
signup()
|
| 121 |
-
|
|
|
|
|
|
|
|
|
|
| 122 |
|
| 123 |
|
| 124 |
main()
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from home import dashboard
|
| 3 |
from streamlit_option_menu import option_menu
|
|
|
|
|
|
|
| 4 |
import pymongo
|
| 5 |
from dotenv import load_dotenv
|
| 6 |
import os
|
|
|
|
| 14 |
uri = os.environ["MONGO_CONNECTION_STRING"]
|
| 15 |
|
| 16 |
# Create a new client and connect to the server
|
| 17 |
+
client = MongoClient(uri, tlsCertificateKeyFile="cert.pem")
|
| 18 |
|
| 19 |
+
db = client["mydata"]
|
| 20 |
|
| 21 |
col = db["Users"]
|
| 22 |
|
|
|
|
| 34 |
# st.title("Authentication")
|
| 35 |
|
| 36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
def login():
|
| 39 |
st.title("Login")
|
|
|
|
| 41 |
usrname = st.text_input("Username")
|
| 42 |
password = st.text_input("Password", type="password")
|
| 43 |
if st.button("Login", key="loginkey"):
|
| 44 |
+
users = list(col.find())
|
| 45 |
+
for user in users:
|
| 46 |
if usrname == user["username"] and password == user["password"]:
|
| 47 |
st.success("Logged in as {}".format(usrname))
|
| 48 |
st.session_state["user"] = "logged"
|
|
|
|
| 64 |
confirm_password = st.text_input("Confirm Password", type="password")
|
| 65 |
if st.button("Signup", key="signupkey"):
|
| 66 |
if password == confirm_password:
|
|
|
|
| 67 |
newuser = {
|
| 68 |
"username": username,
|
| 69 |
+
"password": password
|
|
|
|
| 70 |
}
|
| 71 |
+
col.insert_one(newuser)
|
|
|
|
| 72 |
st.success("Account created! You can now login.")
|
| 73 |
st.snow()
|
| 74 |
st.cache_data.clear()
|
|
|
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
+
if st.session_state["user"] == "visitor":
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
option = option_menu(
|
| 88 |
menu_title="Authentication",
|
|
|
|
| 97 |
login()
|
| 98 |
elif option == "Signup":
|
| 99 |
signup()
|
| 100 |
+
elif st.session_state["user"] == "logged":
|
| 101 |
+
dashboard()
|
| 102 |
+
|
| 103 |
+
|
| 104 |
|
| 105 |
|
| 106 |
main()
|