import streamlit as st
import pandas as pd
import webbrowser as web
username=st.text_input(“Please input your username:”)
pwd=st.text_input(“Please input your password”,type=‘password’)
db = pymysql.connect(host=“localhost”, user=“root”, password=“abcde”, database=“python”, charset=“utf8”)
sql=“select * from pwd”
cursor = db.cursor()
st.info(“You do not have input user name yet”)
st.info(“You do not have input password yet”)
if username in df.values and pwd in df.values:
st.warning(“Sorry, you can not login in, please check your user name or password.”)
import streamlit as st
Thanks for this. I have a question through. Let say if you are running in local host 8501 and if multiple users login how do you differentiate?
you can save all of the user name and their password on your database
Its not about the database. Lets assume i have all the users in my database. But if another user try to connect to the same host (Say 8501) - it will not take you to loginpage. because the previous user is already connected and logged in.
do not worry, web page support many users to view and do operation at same time, their operation is independent.
the code in the example, only when user name and password match, the user can login.
at other condition, the login will fail.
Here is a simple way to add some authentication without using any other packages.
access_token = st.text_input(‘Please enter your access token:’)
if access_token != ‘your_secret_token’:
st.write(‘Sorry, your token is invalid. Please try again or contact the administrator.’)
Do anyone know whether this approach poses any major vulnerability compared to the other ways suggested in other posts?
@filiagees Can you share the authentication code using oauth2-proxy?
How you can implement a logout button in streamlit using this method?
Just get them to delete the password from the text box.
Does someone have a code to share implementing user login and logout buttons using the new Session State feature? It would be really helpful
I don’t know if it’s possible to persist state after page refreshing, i.e., make possible log-in/log-out buttons. (It seems that Streamlit doesn’t support browser cookies yet.) However, if someone needs basic password protection, I created a basic example showing how to restrict access to the dashboard using snippets shared on this thread.
If you know a better way of doing this, please let me know! Would be great to have “proper” authentication instead of DIY hacks. Even enabling cookies would help a lot.
I see that now session state is available via public API.
Hey thanks @devforfu , that is the reason of my question, I’d like to know if there is a way to implement login/logout with the new built-in session state component. Could you update your code to accomplish that?
hi @filiagees pls can you share your demo code?
Hi @devforfu, @Luigi8735, @GitHunter0 You are welcome to look at the secure app example as part of Hydralit, the example app shows how to use any source of login and security level data you want to control different levels of access to multiple “pages” within your application. The example app implements a simple use case that could be expanded easily for more complicated or granular access control. You can even play with the live running demo here.
Hi @Probability, thanks a lot for your suggestion, however, I was looking more for something that @filiagees showed in which I can log in with my Gmail credential (via oauth2-proxy) rather than create new username and password…may you help with that? thanks in advance
Hey @Probability, it looks great! Thank you for sharing