if "auth" not in st.session_state:
oauth2 = OAuth2Component(
client_id = CLIENT_ID,
client_secret = CLIENT_SECRET,
authorize_endpoint = AUTHORIZATION_URL,
token_endpoint = TOKEN_URL,
refresh_token_endpoint = TOKEN_URL,
revoke_token_endpoint = REVOKE_URL,
)
result = oauth2.authorize_button(
name = "Login",
redirect_uri = REDIRECT_URI,
scope = SCOPE,
key = "auth_button",
use_container_width = False,
extras_params={"prompt": "login", "access_type": "offline"},
pkce = None,
)
if result:
st.write(result)
id_token = result["token"]["id_token"]
payload = id_token.split(".")[1]
# add padding to the payload if needed
payload += "=" * (-len(payload) % 4)
payload = json.loads(base64.b64decode(payload))
email = payload["email"]
st.session_state["auth"] = email
st.session_state["token"] = result["token"]
st.rerun()
else:
auth_timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
auth_email = st.session_state["auth"]
st.write("You are logged in!")
st.write("Account: ", auth_email)
st.write("Timestamp: ", auth_timestamp)
if st.button('Logout'):
del st.session_state["auth"]
del st.session_state["token"]
I used the above code to create an authentication button. When I single-clicked it and entered my credentials, it worked as expected. However, if I multiple-clicked it (No idea why, but just in case.) and then entered my credentials, it would end up with the following error:
File "...\.venv\Lib\site-packages\streamlit\runtime\scriptrunner\exec_code.py", line 88, in exec_func_with_error_handling
result = func()
^^^^^^File "...\.venv\Lib\site-packages\streamlit\runtime\scriptrunner\script_runner.py", line 579, in code_to_exec
exec(code, module.__dict__)File "...\app.py", line 39, in <module>
result = oauth2.authorize_button(
^^^^^^^^^^^^^^^^^^^^^^^^File "....venv\Lib\site-packages\streamlit_oauth\__init__.py", line 113, in authorize_button
raise StreamlitOauthError(f"STATE {state} DOES NOT MATCH OR OUT OF DATE")
I can write try-except block to ‘get around’ the issue. However, I am wondering if there is a way to directly fix this? Thank you!