File size: 3,108 Bytes
0a3fd1d
 
 
 
 
7afc158
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0a3fd1d
 
60686d3
0a3fd1d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3df814b
0a3fd1d
 
 
 
 
5bfab8c
0a3fd1d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d75866e
0a3fd1d
 
 
 
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import streamlit as st
from home import dashboard
from streamlit_option_menu import option_menu
import json
import uuid
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"]

# Create a new client and connect to the server
client = MongoClient(uri)

db = client["Cosmo"]

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 load_json():
   with open("database/data.json") as file:
        data = json.load(file)
        return data
        


    

def save_json():
 with open("database/data.json", "w") as file:
        json.dump(data, file, indent=4)



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()
    st.balloons()


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)
            json.dump(data, open("database/data.json", "w"), indent=4)
            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"] == "logged":
        dashboard()

    elif 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()
            


main()