artintel235 commited on
Commit
6e31aea
·
verified ·
1 Parent(s): 86035bf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -6
app.py CHANGED
@@ -130,12 +130,60 @@ def login_callback():
130
  except Exception as e:
131
  st.error(f"Login failed: {e}")
132
 
133
- # Callback for logout
134
- def logout_callback():
135
- st.session_state.logged_in = False
136
- st.session_state.current_user = None
137
- st.session_state.display_name = None
138
- st.info("Logged out successfully!")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
139
 
140
  # Function to get image from url
141
  def get_image_from_url(url):
 
130
  except Exception as e:
131
  st.error(f"Login failed: {e}")
132
 
133
+ # Callback for login
134
+ def login_callback():
135
+ login_identifier = st.session_state.login_identifier
136
+ password = st.session_state.login_password
137
+ try:
138
+ # Try to sign in the user programmatically to check the password validity
139
+ url = f'https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key={FIREBASE_API_KEY}'
140
+ data = {
141
+ 'email': login_identifier,
142
+ 'password': password,
143
+ 'returnSecureToken': True
144
+ }
145
+ response = requests.post(url, json=data)
146
+ result = response.json()
147
+
148
+ if 'idToken' in result:
149
+ # If sign in was successful, then use email to fetch the user
150
+ user = auth.get_user_by_email(login_identifier)
151
+ st.session_state.logged_in = True
152
+ st.session_state.current_user = user.uid
153
+ st.session_state.display_name = user.display_name # Store the display name
154
+ st.success("Logged in successfully!")
155
+
156
+ elif 'error' in result:
157
+ # If sign-in fails, retrieve user using display name
158
+ try:
159
+ user_list = auth.list_users()
160
+ for user_info in user_list.users:
161
+ if user_info.display_name == login_identifier:
162
+ user = user_info
163
+ # If user is found using display name, try signing in using email
164
+ url = f'https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key={FIREBASE_API_KEY}'
165
+ data = {
166
+ 'email': user.email,
167
+ 'password': password,
168
+ 'returnSecureToken': True
169
+ }
170
+ response = requests.post(url, json=data)
171
+ result = response.json()
172
+ if 'idToken' in result:
173
+ st.session_state.logged_in = True
174
+ st.session_state.current_user = user.uid
175
+ st.session_state.display_name = user.display_name # Store the display name
176
+ st.success("Logged in successfully!")
177
+ return
178
+
179
+ raise Exception("User not found with provided credentials.") # if not found, raise exception.
180
+ except Exception as e:
181
+ st.error(f"Login failed: {e}") # if any error, display this message.
182
+ else:
183
+ raise Exception("Error with sign-in endpoint") # If sign-in endpoint doesn't return error or id token, then throw this error.
184
+
185
+ except Exception as e:
186
+ st.error(f"Login failed: {e}")
187
 
188
  # Function to get image from url
189
  def get_image_from_url(url):