artintel235 commited on
Commit
10dce96
·
verified ·
1 Parent(s): ce1a6c4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +81 -6
app.py CHANGED
@@ -1,12 +1,87 @@
1
  import os
2
  import json
3
-
 
 
4
  # Retrieve the JSON string from the secret
5
  json_str = os.getenv("FIREBASE_CREDENTIALS")
6
 
7
- # Convert the string back to a JSON object
8
- if json_str:
9
- firebase_creds = json.loads(json_str)
10
- print("Firebase credentials loaded successfully!")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  else:
12
- print("Failed to load Firebase credentials.")
 
 
1
  import os
2
  import json
3
+ import streamlit as st
4
+ import firebase_admin
5
+ from firebase_admin import credentials, auth
6
  # Retrieve the JSON string from the secret
7
  json_str = os.getenv("FIREBASE_CREDENTIALS")
8
 
9
+ # Initialize Firebase (only once)
10
+ if not firebase_admin._apps:
11
+ cred = credentials.Certificate("path/to/your/serviceAccountKey.json") # Replace with your Firebase service account key
12
+ firebase_admin.initialize_app(cred)
13
+
14
+ # Initialize session state
15
+ if "logged_in" not in st.session_state:
16
+ st.session_state.logged_in = False
17
+ if "current_user" not in st.session_state:
18
+ st.session_state.current_user = None
19
+
20
+ # Callback for registration
21
+ def register_callback():
22
+ email = st.session_state.reg_email
23
+ password = st.session_state.reg_password
24
+ try:
25
+ # Create a new user in Firebase
26
+ user = auth.create_user(email=email, password=password)
27
+ st.success("Registration successful! Please log in.")
28
+ except Exception as e:
29
+ st.error(f"Registration failed: {e}")
30
+
31
+ # Callback for login
32
+ def login_callback():
33
+ email = st.session_state.login_email
34
+ password = st.session_state.login_password
35
+ try:
36
+ # Authenticate user with Firebase
37
+ user = auth.get_user_by_email(email)
38
+ st.session_state.logged_in = True
39
+ st.session_state.current_user = user.uid # Store the user UID in session state
40
+ st.success("Logged in successfully!")
41
+ except Exception as e:
42
+ st.error(f"Login failed: {e}")
43
+
44
+ # Callback for logout
45
+ def logout_callback():
46
+ st.session_state.logged_in = False
47
+ st.session_state.current_user = None
48
+ st.info("Logged out successfully!")
49
+
50
+ # Registration form
51
+ def registration_form():
52
+ with st.form("Registration"):
53
+ st.subheader("Register")
54
+ email = st.text_input("Email", key="reg_email")
55
+ password = st.text_input("Password (min 6 characters)", type="password", key="reg_password")
56
+ submit_button = st.form_submit_button("Register", on_click=register_callback)
57
+
58
+ # Login form
59
+ def login_form():
60
+ with st.form("Login"):
61
+ st.subheader("Login")
62
+ email = st.text_input("Email", key="login_email")
63
+ password = st.text_input("Password", type="password", key="login_password")
64
+ submit_button = st.form_submit_button("Login", on_click=login_callback)
65
+
66
+ # Main app screen (after login)
67
+ def main_app():
68
+ st.subheader(f"Welcome, {st.session_state.current_user}!")
69
+ st.write("Enter a name below to get a greeting.")
70
+
71
+ # Input field for name
72
+ name = st.text_input("Enter a name", key="name_input")
73
+
74
+ # Display greeting in real-time
75
+ if name:
76
+ st.write(f"Hello {name}!")
77
+
78
+ # Logout button
79
+ if st.button("Logout", on_click=logout_callback):
80
+ pass
81
+
82
+ # Render the appropriate screen based on login status
83
+ if st.session_state.logged_in:
84
+ main_app()
85
  else:
86
+ registration_form()
87
+ login_form()