How to add user authentication and keep it logged in

Hi, I would like some help, please.

I’m trying to make my app private, so only a certain user with its password can have access to the data provided in the application. The reason is that we are using confidential data.

So, since Streamlit reads the code every time from the very beginning, how can I keep my app running, without asking the user its username and password more than once?

Every time I change some widget, the runs all over again. And, @st.cache was not built to keep a user logged in if I’m right.

Thank you very much, in advance.

Here is my code:

`def is_authenticated1(user):
return user == “pedro@pedro.com

def is_authenticated2(password):
return password == “admin”

def generate_login_block():
block1 = st.empty()
block2 = st.empty()

return block1, block2

def clean_blocks(blocks):
for block in blocks:
block.empty()

def login1(blocks):
return blocks[0].text_input(“Email”)

def login2(blocks):
return blocks[1].text_input(“Password:”, value="", type=“password”)

def main():
** here is the function that runs in the app

login_blocks = generate_login_block()
user = login1(login_blocks)

if is_authenticated1(user):
password = login2(login_blocks)
if is_authenticated2(password):
clean_blocks(login_blocks)
main()
elif password:
st.info(“Password incorrect”)
elif user:
st.info(“Email incorrect”)`

Synode has a great sessionState implementation.

You could use it to store an entered username and/or password and compare it against either hardcoded users or against a database for authentication. Session state variables persist until the web page is refreshed.

1 Like