Spaces:
Sleeping
Sleeping
app.py
CHANGED
|
@@ -98,22 +98,18 @@ def load_data():
|
|
| 98 |
|
| 99 |
# Main function to run the Streamlit app
|
| 100 |
def main():
|
|
|
|
|
|
|
|
|
|
| 101 |
if not is_logged_in():
|
| 102 |
-
st.title('π Student Grade Lookup')
|
| 103 |
-
st.markdown("---")
|
| 104 |
st.write("Please log in with your CSPC Google account to access the grade lookup.")
|
| 105 |
|
| 106 |
if st.button("Login with Google"):
|
| 107 |
flow = create_flow()
|
| 108 |
-
authorization_url,
|
| 109 |
-
st.session_state['oauth_state'] = state
|
| 110 |
-
st.session_state['oauth_state_time'] = time.time()
|
| 111 |
-
st.experimental_set_query_params(state=state)
|
| 112 |
st.markdown(f"[Login with Google]({authorization_url})")
|
| 113 |
else:
|
| 114 |
-
st.
|
| 115 |
-
st.markdown("---")
|
| 116 |
-
|
| 117 |
# Load the data
|
| 118 |
df = load_data()
|
| 119 |
|
|
@@ -184,58 +180,25 @@ def main():
|
|
| 184 |
|
| 185 |
if st.button("Logout"):
|
| 186 |
del st.session_state['credentials']
|
| 187 |
-
st.
|
| 188 |
|
| 189 |
# Function to handle OAuth callback
|
| 190 |
def handle_callback():
|
| 191 |
flow = create_flow()
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
if time.time() - st.session_state['oauth_state_time'] > 600: # 10 minutes expiration
|
| 203 |
-
logging.error("OAuth state has expired")
|
| 204 |
-
st.error("Authentication session expired. Please try again.")
|
| 205 |
-
return
|
| 206 |
-
|
| 207 |
-
if state != st.session_state['oauth_state']:
|
| 208 |
-
logging.error("OAuth state mismatch")
|
| 209 |
-
st.error("Authentication failed. Please try again.")
|
| 210 |
-
return
|
| 211 |
-
|
| 212 |
-
try:
|
| 213 |
-
logging.debug(f"Attempting to fetch token with code: {code}")
|
| 214 |
-
flow.fetch_token(code=code)
|
| 215 |
-
credentials = flow.credentials
|
| 216 |
-
st.session_state['credentials'] = credentials.to_json()
|
| 217 |
-
logging.debug("Token fetch successful")
|
| 218 |
-
del st.session_state['oauth_state']
|
| 219 |
-
del st.session_state['oauth_state_time']
|
| 220 |
-
st.experimental_set_query_params() # Clear query params
|
| 221 |
-
return credentials
|
| 222 |
-
except InvalidGrantError as e:
|
| 223 |
-
logging.error(f"InvalidGrantError: {str(e)}")
|
| 224 |
-
st.error("The authorization code has expired or is invalid. Please try logging in again.")
|
| 225 |
-
if 'credentials' in st.session_state:
|
| 226 |
-
del st.session_state['credentials']
|
| 227 |
-
except Exception as e:
|
| 228 |
-
logging.error(f"Unexpected error during token fetch: {str(e)}")
|
| 229 |
-
st.error(f"An unexpected error occurred: {str(e)}")
|
| 230 |
-
else:
|
| 231 |
-
logging.warning("No authorization code or state found in the URL parameters.")
|
| 232 |
-
st.error("Authentication failed. Please try again.")
|
| 233 |
|
| 234 |
if __name__ == '__main__':
|
| 235 |
logging.debug("Starting the application")
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
logging.debug("Authorization code and state found in query parameters")
|
| 239 |
handle_callback()
|
| 240 |
-
st.rerun()
|
| 241 |
main()
|
|
|
|
| 98 |
|
| 99 |
# Main function to run the Streamlit app
|
| 100 |
def main():
|
| 101 |
+
st.title('π Student Grade Lookup')
|
| 102 |
+
st.markdown("---")
|
| 103 |
+
|
| 104 |
if not is_logged_in():
|
|
|
|
|
|
|
| 105 |
st.write("Please log in with your CSPC Google account to access the grade lookup.")
|
| 106 |
|
| 107 |
if st.button("Login with Google"):
|
| 108 |
flow = create_flow()
|
| 109 |
+
authorization_url, _ = flow.authorization_url(prompt="consent")
|
|
|
|
|
|
|
|
|
|
| 110 |
st.markdown(f"[Login with Google]({authorization_url})")
|
| 111 |
else:
|
| 112 |
+
st.write("You are logged in.")
|
|
|
|
|
|
|
| 113 |
# Load the data
|
| 114 |
df = load_data()
|
| 115 |
|
|
|
|
| 180 |
|
| 181 |
if st.button("Logout"):
|
| 182 |
del st.session_state['credentials']
|
| 183 |
+
st.experimental_rerun()
|
| 184 |
|
| 185 |
# Function to handle OAuth callback
|
| 186 |
def handle_callback():
|
| 187 |
flow = create_flow()
|
| 188 |
+
try:
|
| 189 |
+
flow.fetch_token(code=st.experimental_get_query_params()["code"][0])
|
| 190 |
+
credentials = flow.credentials
|
| 191 |
+
st.session_state['credentials'] = credentials.to_json()
|
| 192 |
+
logging.debug("Token fetch successful")
|
| 193 |
+
st.success("Authentication successful!")
|
| 194 |
+
return credentials
|
| 195 |
+
except Exception as e:
|
| 196 |
+
logging.error(f"Error during authentication: {str(e)}")
|
| 197 |
+
st.error(f"Authentication failed: {str(e)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 198 |
|
| 199 |
if __name__ == '__main__':
|
| 200 |
logging.debug("Starting the application")
|
| 201 |
+
if 'code' in st.experimental_get_query_params():
|
| 202 |
+
logging.debug("Authorization code found in query parameters")
|
|
|
|
| 203 |
handle_callback()
|
|
|
|
| 204 |
main()
|