Skip to content

Commit

Permalink
add unique ids
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh-XT committed Jun 13, 2024
1 parent 94e775c commit 75203ef
Showing 1 changed file with 59 additions and 13 deletions.
72 changes: 59 additions & 13 deletions components/Auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,31 @@ def google_sso_button():
code = ""

# Use cookies to persist state
oauth2_token_requested = get_cookie("oauth2_token_requested")
oauth2_token_completed = get_cookie("oauth2_token_completed")
oauth2_redirect_url = get_cookie("oauth2_redirect_url")
oauth2_token_requested = get_cookie(
"oauth2_token_requested", key="getCookie_oauth2_token_requested"
)
oauth2_token_completed = get_cookie(
"oauth2_token_completed", key="getCookie_oauth2_token_completed"
)
oauth2_redirect_url = get_cookie(
"oauth2_redirect_url", key="getCookie_oauth2_redirect_url"
)

if oauth2_token_requested is None:
oauth2_token_requested = "False"
set_cookie("oauth2_token_requested", "False", 1)
set_cookie(
"oauth2_token_requested", "False", 1, key="setCookie_oauth2_token_requested"
)

if oauth2_token_completed is None:
oauth2_token_completed = "False"
set_cookie("oauth2_token_completed", "False", 1)
set_cookie(
"oauth2_token_completed", "False", 1, key="setCookie_oauth2_token_completed"
)

if oauth2_redirect_url is None:
oauth2_redirect_url = ""
set_cookie("oauth2_redirect_url", "", 1)
set_cookie("oauth2_redirect_url", "", 1, key="setCookie_oauth2_redirect_url")

st.write(f"Code received: {code}") # Debug message
st.write(f"Requested (cookie): {oauth2_token_requested}") # Debug message
Expand All @@ -82,8 +92,19 @@ def google_sso_button():
else:
if code != "" and oauth2_token_requested == "False":
with lock:
if get_cookie("oauth2_token_requested") == "False":
set_cookie("oauth2_token_requested", "True", 1)
if (
get_cookie(
"oauth2_token_requested",
key="getCookie_oauth2_token_requested_check",
)
== "False"
):
set_cookie(
"oauth2_token_requested",
"True",
1,
key="setCookie_oauth2_token_requested_update",
)
st.write("Making request to backend...") # Debug message
response = requests.post(
f"{auth_uri}/v1/oauth2/google",
Expand All @@ -103,21 +124,46 @@ def google_sso_button():
if "detail" in data:
new_uri = data["detail"]
st.write(f"Redirecting to: {new_uri}") # Debug message
set_cookie("oauth2_redirect_url", new_uri, 1)
set_cookie("oauth2_token_completed", "True", 1)
set_cookie("oauth2_token_requested", "False", 1)
set_cookie(
"oauth2_redirect_url",
new_uri,
1,
key="setCookie_oauth2_redirect_url_update",
)
set_cookie(
"oauth2_token_completed",
"True",
1,
key="setCookie_oauth2_token_completed_update",
)
set_cookie(
"oauth2_token_requested",
"False",
1,
key="setCookie_oauth2_token_requested_reset",
)
# Use JavaScript to redirect immediately
st.write(
f'<script>window.location.href = "{new_uri}";</script>',
unsafe_allow_html=True,
)
st.stop()
else:
set_cookie("oauth2_token_requested", "False", 1)
set_cookie(
"oauth2_token_requested",
"False",
1,
key="setCookie_oauth2_token_requested_error",
)
st.error("Unexpected response structure from backend.")
st.stop()
else:
set_cookie("oauth2_token_requested", "False", 1)
set_cookie(
"oauth2_token_requested",
"False",
1,
key="setCookie_oauth2_token_requested_error_400",
)
st.error(response.json()["detail"])
st.stop()
elif oauth2_token_completed == "True":
Expand Down

0 comments on commit 75203ef

Please sign in to comment.