User authentication

import streamlit as st
import pymysql
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()
cursor.execute(sql)
db.commit()
df=pd.read_sql(sql,con=db)
if username=="":
st.info(“You do not have input user name yet”)
elif pwd=="":
st.info(“You do not have input password yet”)
else:
if username in df.values and pwd in df.values:
st.success(“Welcome back!”)
web.open(“https://www.baidu.com/”)
else:
st.warning(“Sorry, you can not login in, please check your user name or password.”)

1 Like

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?

1 Like

you can save all of the user name and their password on your database

1 Like

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.

1 Like

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.

1 Like