I am creating a single page st app with login and logout capabilities.
I am using state to check whether a user is authenticated.
When the page loads, all is well. Login form appears
Creds are inputted and then checked against db.
If session_state[‘authenticator’] ==True then other components appear as well as a Logout button.
The problem was that the login form stayed there unless I clicked on Login again (!).
When I clicked on Logout, the other components disappeared, Logout stayed but Login didn’t reappear. When I clicked Logout again then the logout button disappeared and login form appeared.
To solve these problems I used st.rerun() so that the page was re-rendered.
I thought that every time someone clicks on a button or updates a text input etc. the script would re-run anyway. Am I missing something? How is script re-run and page refresh works with streamlit?
I am attaching a redacted version of the code:
import streamlit as st
def verify_creds(email,password):
results = [{"email": "j.j@a.a", "password": "1234"}] #querying the db for records with given email.
results = list(results)
if len(results) > 1:
st.warning("Multiple users found with the same email and password. Please contact support.")
st.session_state['authentication_status'] = False
elif len(results) == 1:
user = results[0]
if user["password"] == password:
st.session_state['authentication_status'] = True
st.success("Logged in")
#st.rerun()
else:
st.session_state['authentication_status'] = False
st.warning("Incorrect password")
else:
st.session_state['authentication_status'] = False
st.warning("User not found")
return 0
if 'authentication_status' not in st.session_state:
st.session_state['authentication_status'] = None
if not st.session_state['authentication_status']:
login_form = st.form('Login')
email = login_form.text_input('Email').lower()
st.session_state['email'] = email
password = login_form.text_input('Password', type='password')
if login_form.form_submit_button('Login'):
verify_creds(email,password)
if st.session_state['authentication_status']:
if st.button("Logout"):
st.session_state['authentication_status'] = False
st.success("Logged out")
#st.rerun()
else:
st.write('You are seeing the logged in "version" of the app.')