-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth_management.py
95 lines (85 loc) · 3.86 KB
/
auth_management.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import os
from supabase import create_client
import streamlit as st
# Load credentials from st.secrets
supabase_url = st.secrets["supabase"]["url"]
supabase_anon_key = st.secrets["supabase"]["anon_key"]
supabase_service_role_key = st.secrets["supabase"]["service_role_key"] # Add service role key
supabase = create_client(supabase_url, supabase_anon_key)
service_supabase = create_client(supabase_url, supabase_service_role_key) # Create service role client
def authenticate_user():
st.title("Login")
email = st.text_input("Email")
password = st.text_input("Password", type="password")
if st.button("Login"):
response = None # Initialize response variable
try:
response = supabase.auth.sign_in_with_password({"email": email, "password": password})
if response and response.user:
user = response.user
st.success(f"Welcome, {user.email}!")
st.session_state.user = user # Update session state
return user
else:
st.error("Invalid credentials.")
except Exception as e:
st.error(f"Error: {e}")
st.write(f"Exception details: {e}") # Detailed exception information
st.write(f"Supabase response: {response}") # Log the full response
return None
def signup_user():
st.title("Sign Up")
email = st.text_input("Email")
password = st.text_input("Password", type="password")
organization = st.text_input("Organization Name")
if st.button("Sign Up"):
response = None # Initialize response variable
try:
response = supabase.auth.sign_up({"email": email, "password": password})
if response and response.user:
# Create organization record
org_response = supabase.table("organizations").insert({
"name": organization,
"admin_user_id": response.user.id
}).execute()
if org_response.error:
st.error(f"Error creating organization: {org_response.error}")
else:
st.success("Account created successfully! Please log in.")
else:
st.error("Error signing up. Please try again.")
except Exception as e:
if "User already registered" in str(e):
st.error("User already exists. Please log in.")
else:
st.error(f"Error: {e}")
def create_superuser():
"""
Pre-create a superuser for the app.
"""
email = "[email protected]"
password = st.secrets["superuser"]["password"]
# Check if the superuser already exists
response = supabase.auth.sign_in_with_password({"email": email, "password": password})
if response and response.user:
user = response.user
service_supabase.table("auth.users").update({"is_superuser": True}).eq("id", user.id).execute()
st.write("Superuser already exists.")
else:
# Create a new superuser
response = supabase.auth.sign_up({"email": email, "password": password})
if response and response.user:
user = response.user
service_supabase.table("auth.users").update({"is_superuser": True}).eq("id", user.id).execute()
st.write("Superuser created successfully!")
# Create the "FitTech" organization and assign it to the superuser
org_response = supabase.table("organizations").insert({
"name": "FitTech",
"admin_user_id": user.id
}).execute()
if org_response.data:
service_supabase.table("auth.users").update({"organization_id": org_response.data[0]["id"]}).eq("id", user.id).execute()
st.write("Organization 'FitTech' created and assigned to the superuser.")
return user
st.write("Failed to create superuser.")
return None