File size: 2,403 Bytes
907daef
 
 
ca713d9
 
907daef
ca713d9
 
 
 
 
d4b9859
 
 
 
 
907daef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
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[""]

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:
            data = json.load(open("database/data.json"))
            newuser = {
                "username": username,
                "password": password,
                 "id": str(uuid.uuid4())
            }
            data["users"].append(newuser)
            with open("database/data.json", "w") as file:
                json.dump(data, file)
            st.success("Account created")
        else:            
             st.error("Passwords do not match")
           
             
def main():
     if "user" not in st.session_state:
        st.session_state["user"] = "visitor"
        if st.session_state["user"] == "logged":
            dashboard()
        elif st.session_state["user"] == "visitor":
            option = option_menu(
                menu_title="Authentication",
                options=["Login", "Signup"],
            )
            
            if option == "Login":
                login()
            elif option == "Signup":
                signup()
                
                
main()