If you’re creating a debugging post, please include the following info:
- Are you running your app locally or is it deployed?
App is running locally - If your app is deployed:
a. Is it deployed on Community Cloud or another hosting platform?
b. Share the link to the public deployed app. - Share the link to your app’s public GitHub repository (including a requirements file).
- Share the full text of the error message (not a screenshot).
I’m running into this weird issue and was hoping someone can shed light on this.
I’m using streamlit in combination with mysql and using st.session_state and st.query_params()
login.py has the following code - once a user logs in (login and not register), they would be directed to home screen which has the main side bar and that will display bunch of pages (using st.page_link())
def main():
token = st.query_params.get("session_token", None)
print(f"token now is {st.query_params}")
if st.query_params and token is not None:
print(f'refreshing token: {token}')
user = get_user_by_token(token)
if user:
st.session_state.logged_in = True
st.session_state.email = user
else:
st.session_state.logged_in = False
else:
print("token is empty")
st.session_state.logged_in = False
if not st.session_state.logged_in:
with st.container(border=True):
st.title("Login")
if st.button("Signup"):
st.switch_page('pages/register.py')
email = st.text_input("Email")
password = st.text_input("Password", type="password")
login_button = st.button("Login")
forgot_pwd_button = st.button("Forgot Password")
if login_button and email and password:
if validate_user(email, password):
token = generate_token()
update_token(email, token)
st.query_params["session_token"] = token
st.session_state['email'] = email
st.session_state['logged_in'] = True
time.sleep(2)
# st.rerun()
st.switch_page('Home.py')
else:
st.error("Invalid email or password")
elif forgot_pwd_button:
st.switch_page('pages/reset.py')
else:
st.switch_page("Home.py")
home.py
ef make_sidebar():
st.write(st.session_state)
with st.sidebar:
# token = st.query_params.get("session_token", None)
token = st.session_state and 'session_token' in st.session_state
if token:
st.query_params.update({"session_token": st.session_state.session_token})
token = st.query_params.get("session_token", None)
print(f'refresh token: {token}')
if token is not None:
print(f"here here here {token}, {st.session_state}")
user = get_user_by_token(token)
if user:
st.session_state.logged_in = True
st.session_state.email = user
st.session_state.session_token = token
elif 'session_token' in st.session_state:
token = st.session_state.session_token
st.query_params.update({"session_token": token})
print(f'refresh token now is: {token}')
user = get_user_by_token(token)
st.session_state.logged_in = True
st.session_state.email = user
else:
st.session_state.logged_in = False
if st.session_state and ('logged_in' not in st.session_state or st.session_state.logged_in == False):
st.page_link("Home.py", label="Home", icon="🔒")
st.page_link("pages/login.py", label="Login/Signup", icon="🔒")
else:
st.write(f"Welcome {user}")
The problem is that when I click on login, I can see session_token getting added to the URL (ex: …login?session_token=xxx). As soon as the login is clicked, i’m in the home page and homepage URL doesn’t show session_token. I expect homepage to show URL with the session_token (ex: …home?session_token=xxx). Due to this, when the user refreshes the url, the token is getting lost and it appears as if there is no session_token and hence it goes to the login page.
I will have to click on the home_page on the sidebar after which it shows the URL completely with the session_token. Trying to understand why I have to click home_page again (double click issue?) to get the URL properly. In the even I don’t click on homepage again and refresh the page, it directs to login page as if the login hasn’t happened and forces user to login again.
Is this a known issue with st.query_params()? Thanks!
- Share the Streamlit and Python versions.
Python 3.9.6, streamlit 1.36.0