-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstreamlit_app.py
72 lines (56 loc) · 2.34 KB
/
streamlit_app.py
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
import streamlit as st
from streamlit_extras.switch_page_button import switch_page
import json
st.set_page_config(
page_title="Which-Group-What-Project",
layout="centered",
page_icon=":material/login:",
)
# Local imports
from frontend.utils.login_button import login_button
from frontend.utils.logout_button import logout_action
from backend.auth_flow import authenticate_user
from backend.auth_flow import assign_user_role
from backend import create_supabase_client
from frontend.cookies.cookies import create_cookie_manager
cookies = create_cookie_manager()
st.session_state.cookies = cookies
if not cookies.ready():
st.stop()
# Main program after login
def main():
# Checks if a user is present in the session state
if "user" not in st.session_state:
st.title("Which-Group-What-Project")
with st.spinner("Authenticating user..."):
if "g_session" in cookies.keys() and str(cookies["g_session"]):
g_session = json.loads(cookies.get("g_session"))
user: object = authenticate_user(g_session, cookies)
if user is not None:
if "user_type" in dict(user)["user_metadata"]:
st.session_state.user = dict(user)["user_metadata"]
st.rerun()
email = dict(user)["email"]
email_role_map = {
st.secrets["STUDENT_SUFFIX"]: "student",
st.secrets["TEACHER_SUFFIX"]: "teacher",
st.secrets["ADMIN_1"]: "admin",
st.secrets["ADMIN_2"]: "admin",
}
role_assigned = False
for key, role in email_role_map.items():
if email.endswith(key) or email == key:
response = assign_user_role(role, email)
st.session_state.user = dict(response)["user_metadata"]
st.rerun()
role_assigned = True
break
if not role_assigned:
st.write("You are not allowed to access this app.")
logout_action(cookies)
else:
login_button()
else:
switch_page("dashboard")
create_supabase_client()
main()