This repository was archived by the owner on May 13, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
This repository was archived by the owner on May 13, 2025. It is now read-only.
Refreshed pages not working - button shows logged in but no token in python. #50
Copy link
Copy link
Open
Description
Using the example code, I can successfully log in users and get a token back from MS, but there seems to be a disconnect between the javascript/browser side and the backend python side.
- The button rendered in the browser thinks it's logged in (shows text to "log out")
- There is no
session_state.user
stored in the session (expected b/c page was refreshed) but there is also no value login_token
is not defined in the python.- It's like the javascript doesn't send back the login state/token to streamlit even though it correctly detects the persistence of active MS login.
To correct this out-of-sync issue, users have to click the logout button, refresh the streamlit app (F5 in browser) then restart the login process via MS. Then token is returned to streamlit and everything works fine... until a session is destroyed by a page refresh and the whole cycle must be done again.
import streamlit as st
import traceback
#import streamlit as st
from msal_streamlit_authentication import msal_authentication
def show(session_state):
st.title("Authentication")
if 'user' in st.session_state:
session_state.user = st.session_state['user']
else:
st.write("You are not yet authenticated.")
st.write("Click the 'Log In' button.")
login_token = msal_authentication(
auth={
"clientId": "xxxxxxxxxxxxxxxxxxxxxxxxxxx",
"authority": "xxxxxxxxxxxxxxxxxxxxxxxxxxx",
"redirectUri": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"postLogoutRedirectUri": "/"
}, # Corresponds to the 'auth' configuration for an MSAL Instance
cache={
"cacheLocation": "sessionStorage",
"storeAuthStateInCookie": False
}, # Corresponds to the 'cache' configuration for an MSAL Instance
login_request={
"scopes": ["xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"]
}, # Optional
logout_request={}, # Optional
login_button_text="Login", # Optional, defaults to "Login"
logout_button_text="Logout", # Optional, defaults to "Logout"
class_name="css_button_class_selector", # Optional, defaults to None. Corresponds to HTML class.
html_id="html_id_for_button", # Optional, defaults to None. Corresponds to HTML id.
key=1 # Optional if only a single instance is needed
)
try:
if login_token is None:
st.write("Click the 'Log In' button.")
else:
st.write(login_token.get("account").get("username"))
st.write(login_token.get("account"))
st.session_state['user'] = login_token.get("account").get("username")
st.session_state['login_token'] = login_token
except Exception as e:
st.write("Unable to work with login_token")
# Handle any other exceptions that were not caught by the above
st.write(f"An unexpected error occurred: {e}")
# Print the stack trace
st.write(traceback.format_exc())
aaravind100, danpf and cjohnsto-nz
Metadata
Metadata
Assignees
Labels
No labels