mestreamlit / pages /1003_streamlit_authenticator.py
yuanjie
update
3369999
raw
history blame
2.38 kB
from meutils.pipe import *
import streamlit as st
import streamlit_authenticator as stauth
from streamlit_authenticator import Authenticate
from streamlit_option_menu import option_menu
# https://github.com/mkhorasani/Streamlit-Authenticator
st.set_page_config(page_title="Platform tester", page_icon=":rainbow:", layout="wide", initial_sidebar_state="auto")
# 如下代码数据,可以来自数据库
hashed_passwords = stauth.Hasher(['123', '456']).generate()
_ = f"""
credentials:
usernames:
nesc:
email: [email protected]
name: nesc
password: {hashed_passwords[0]} # To be replaced with hashed password
jsmith:
email: [email protected]
name: John Smith
password: 123 # To be replaced with hashed password
rbriggs:
email: [email protected]
name: Rebecca Briggs
password: 456 # To be replaced with hashed password
cookie:
expiry_days: 30
key: some_signature_key
name: some_cookie_name
preauthorized:
emails:
- [email protected]
"""
config = yaml.load(_)
authenticator = Authenticate(
config['credentials'],
config['cookie']['name'],
config['cookie']['key'],
config['cookie']['expiry_days'],
config['preauthorized']
)
name, authentication_status, username = authenticator.login('Login', 'main')
if authentication_status:
authenticator.logout('Logout', 'main')
st.write(f'Welcome *{name}*')
st.title('Some content')
elif authentication_status == False:
st.error('Username/password is incorrect')
elif authentication_status == None:
st.warning('Please enter your username and password')
if authentication_status:
try:
if authenticator.reset_password(username, 'Reset password'):
st.success('Password modified successfully')
except Exception as e:
st.error(e)
try:
if authenticator.register_user('Register user', preauthorization=False):
st.success('User registered successfully')
except Exception as e:
st.error(e)
try:
username_forgot_pw, email_forgot_password, random_password = authenticator.forgot_password('Forgot password')
if username_forgot_pw:
st.success('New password sent securely')
# Random password to be transferred to user securely
elif username_forgot_pw == False:
st.error('Username not found')
except Exception as e:
st.error(e)