Spaces:
Sleeping
Sleeping
Nikhil0987
commited on
Commit
·
0a3fd1d
0
Parent(s):
HELLO
Browse files
auth.py
ADDED
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
|
7 |
+
|
8 |
+
st.set_page_config(page_title="Authentication", page_icon=":guardsman:", layout="wide")
|
9 |
+
|
10 |
+
# st.title("Authentication")
|
11 |
+
|
12 |
+
|
13 |
+
def load_json():
|
14 |
+
with open("database/data.json") as file:
|
15 |
+
data = json.load(file)
|
16 |
+
return data
|
17 |
+
|
18 |
+
|
19 |
+
|
20 |
+
|
21 |
+
|
22 |
+
def save_json():
|
23 |
+
with open("database/data.json", "w") as file:
|
24 |
+
json.dump(data, file, indent=4)
|
25 |
+
|
26 |
+
|
27 |
+
|
28 |
+
def login():
|
29 |
+
st.title("Login")
|
30 |
+
data = json.load(open("database/data.json"))
|
31 |
+
usrname = st.text_input("Username")
|
32 |
+
password = st.text_input("Password", type="password")
|
33 |
+
if st.button("Login", key="loginkey"):
|
34 |
+
for user in data["users"]:
|
35 |
+
if usrname == user["username"] and password == user["password"]:
|
36 |
+
st.success("Logged in as {}".format(usrname))
|
37 |
+
st.session_state["user"] = "logged"
|
38 |
+
flag = True
|
39 |
+
st.experimental_rerun()
|
40 |
+
else:
|
41 |
+
flag = False
|
42 |
+
if flag == False:
|
43 |
+
st.error("Invalid username or password")
|
44 |
+
st.stop()
|
45 |
+
st.balloons()
|
46 |
+
|
47 |
+
|
48 |
+
def signup():
|
49 |
+
|
50 |
+
st.title("Signup")
|
51 |
+
username = st.text_input("Username")
|
52 |
+
password = st.text_input("Password", type="password")
|
53 |
+
confirm_password = st.text_input("Confirm Password", type="password")
|
54 |
+
if st.button("Signup", key="signupkey"):
|
55 |
+
if password == confirm_password:
|
56 |
+
data = json.load(open("database/data.json"))
|
57 |
+
newuser = {
|
58 |
+
"username": username,
|
59 |
+
"password": password,
|
60 |
+
"id": str(uuid.uuid4())
|
61 |
+
}
|
62 |
+
data["users"].append(newuser)
|
63 |
+
json.dump(data, open("database/data.json", "w"), indent=4)
|
64 |
+
st.success("Account created! You can now login.")
|
65 |
+
st.snow()
|
66 |
+
st.cache_data.clear()
|
67 |
+
else:
|
68 |
+
st.error("Passwords do not match")
|
69 |
+
|
70 |
+
def main():
|
71 |
+
# st.title("Authentication")
|
72 |
+
if "user" not in st.session_state:
|
73 |
+
st.session_state["user"] = "visitor"
|
74 |
+
|
75 |
+
|
76 |
+
|
77 |
+
if st.session_state["user"] == "logged":
|
78 |
+
dashboard()
|
79 |
+
|
80 |
+
elif st.session_state["user"] == "visitor":
|
81 |
+
|
82 |
+
option = option_menu(
|
83 |
+
menu_title="Authentication",
|
84 |
+
options=["Login", "Signup"],
|
85 |
+
icons=["house", "activity"],
|
86 |
+
menu_icon="cast",
|
87 |
+
default_index=0,
|
88 |
+
orientation="horizontal",
|
89 |
+
|
90 |
+
)
|
91 |
+
if option == "Login":
|
92 |
+
login()
|
93 |
+
elif option == "Signup":
|
94 |
+
signup()
|
95 |
+
|
96 |
+
|
97 |
+
main()
|
98 |
+
|