You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
So I was trying to hold a JWT token in cookie when login button is pressed.
app.py
importhydralitashyimportstreamlitasstfromhydralit_componentsimportCookieManagerfromloginimportLoginAppcookie_manager=CookieManager()
app=hy.HydraApp(title='test', favicon="🐙", hide_streamlit_markers=True,
allow_url_nav=True, sidebar_state="expanded",
layout='wide'
)
app.add_app("Login", LoginApp(cookie_manager=cookie_manager), is_login=True, logout_label="Logout")
@app.logout_callbackdefmylogout_cb():
cookie_manager.delete('user_data')
print('I was called from Hydralit at logout!')
@app.login_callbackdefmylogin_cb():
print('I was called from Hydralit at login!')
login.py
classLoginApp(HydraHeadApp):
""" This is an example login application to be used to secure access within a HydraApp streamlit application. This application implementation uses the allow_access session variable and uses the do_redirect method if the login check is successful. """def__init__(self, cookie_manager, title='', **kwargs):
self.__dict__.update(kwargs)
self.title=titleself.cookie_manager=cookie_managerdef_check_cookie_login(self) ->dict|None:
""" Check if the user is logged in. """session_cookie=self.cookie_manager.get("user_data")
ifsession_cookie:
return {'user': 'joe', 'level': 1}
# ToDo: Check parse jwt and check if its valid and then return the user dictdefrun(self) ->None:
""" Application entry point. """login=self._check_cookie_login()
iflogin:
self.set_access(1, login['user'], cache_access=True)
self.do_redirect()
st.markdown("<h1 style='text-align: center;'>Secure Hydralit Login</h1>", unsafe_allow_html=True)
c1, c2, c3, =st.columns([2, 2, 2])
form_data=self._create_login_form(c2)
pretty_btn=""" <style> div[class="row-widget stButton"] > button { width: 100%; } </style> <br><br> """c2.markdown(pretty_btn, unsafe_allow_html=True)
ifform_data['submitted']:
self._do_login(form_data, c2)
@staticmethoddef_create_login_form(parent_container) ->Dict:
login_form=parent_container.form(key="login_form")
form_state= {}
form_state['username'] =login_form.text_input('Username')
form_state['password'] =login_form.text_input('Password', type="password")
form_state['submitted'] =login_form.form_submit_button('Login')
parent_container.write("sample login -> joe & joe")
returnform_statedef_do_login(self, form_data, msg_container) ->None:
# access_level=0 Access denied!access_level=self._check_login(form_data)
ifaccess_level>0:
msg_container.success(f"✔️ Login success")
withst.spinner("🤓 now redirecting to application...."):
time.sleep(1)
self.set_access(1, form_data['username'], cache_access=True)
self.cookie_manager.set("user_data", 'True')
# Do the kick to the home pageself.do_redirect()
else:
self.session_state.allow_access=0self.session_state.current_user=Nonemsg_container.error(f"❌ Login unsuccessful, 😕 please check your username and password and try again.")
def_check_login(self, login_data) ->int:
iflogin_data['username'] =='joe'andlogin_data['password'] =='joe':
return1else:
return0
The above basically checks if there is a cookie with name user_data and logs in automatically if its set.
When the cookie is not found it loads the modified example from example code.
When the username and password matches. It should set a cookie with name user_data but this fails here.
But doing the same using login_callback in app.py works
@app.logout_callbackdefmylogout_cb():
cookie_manager.delete('user_data')
print('I was called from Hydralit at logout!')
@app.login_callbackdefmylogin_cb():
print('I was called from Hydralit at login!')
cookie_manager.set('user_data', "Joe")
Now whenever I reload the page I can skip the login page. But hitting the logout button sends invokes the mylogout_cb function. But cookie_manager.delete('user_data') has no effect and doesn't delete the cookie.
But putting the same cookie_manager.delete('user_data') inside the mylogin_cb callback actually deletes the cookie on next login / on page reload.
Am i missing something here ?. What would be the best way for me to achieve this. I tried extra_streamlit_components library and it has the same behaviour
The text was updated successfully, but these errors were encountered:
Just gave https://github.com/ktosiek/streamlit-cookies-manager a try. everything works as expected. Cookies get saved when login button is pressed (code for saving the jwt cookie is in the LoginApp).
Cookies get deleted when logout_callback is invoked.
How are you able to achieve a logout button in your code? I am able to login but I get an error saying that an app named Logout doesn't exist. What could I do to rectify this error ?
So I was trying to hold a JWT token in cookie when login button is pressed.
app.py
login.py
The above basically checks if there is a cookie with name
user_data
and logs in automatically if its set.When the cookie is not found it loads the modified example from example code.
When the username and password matches. It should set a cookie with name
user_data
but this fails here.But doing the same using login_callback in app.py works
Now whenever I reload the page I can skip the login page. But hitting the logout button sends invokes the
mylogout_cb
function. Butcookie_manager.delete('user_data')
has no effect and doesn't delete the cookie.But putting the same
cookie_manager.delete('user_data')
inside themylogin_cb
callback actually deletes the cookie on next login / on page reload.Am i missing something here ?. What would be the best way for me to achieve this. I tried extra_streamlit_components library and it has the same behaviour
The text was updated successfully, but these errors were encountered: