Streamlit Authenticator

0.4.2 · active · verified Thu Apr 16

Streamlit Authenticator (current version 0.4.2) is a Python library that provides a secure authentication module to manage user access in Streamlit applications. It offers various widgets for login, logout, user registration, password reset, and user detail modification, supporting both local credential management and integration with OAuth2 providers. The library is actively maintained with frequent releases introducing new features and improvements.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to set up basic username/password authentication using `streamlit-authenticator`. It initializes the authenticator with credentials (hardcoded for brevity, but typically loaded from a `config.yaml` file), displays a login widget, and then shows protected content upon successful authentication. It also includes a logout button. For a real application, replace hardcoded `config` with loading from a `config.yaml` and consider storing sensitive keys in environment variables.

import streamlit as st
import streamlit_authenticator as stauth
import yaml
from yaml.loader import SafeLoader

# --- User Credentials & Cookie Configuration (typically from config.yaml) ---
config = {
    'credentials': {
        'usernames': {
            'john_doe': {
                'email': 'john@example.com',
                'name': 'John Doe',
                'password': 'abc' # Will be hashed if auto_hash is True
            },
            'rebecca_smith': {
                'email': 'rebecca@example.com',
                'name': 'Rebecca Smith',
                'password': 'def'
            }
        }
    },
    'cookie': {
        'expiry_days': 30,
        'key': 'random_signature_key_here',
        'name': 'my_app_cookie'
    }
}

# To simulate loading from file (for demonstration, usually you'd load from actual config.yaml)
# In a real app, you would load this from a persistent config.yaml file.
# Example: with open('config.yaml') as file:
#              config = yaml.load(file, Loader=SafeLoader)

# Hash passwords only if not already hashed (auto_hash=True by default in Authenticate)
# You can pre-hash them explicitly if you wish:
# for username, user_data in config['credentials']['usernames'].items():
#    user_data['password'] = stauth.Hasher([user_data['password']]).generate()[0]

# --- Initialize Authenticator ---
authenticator = stauth.Authenticate(
    config['credentials'],
    config['cookie']['name'],
    config['cookie']['key'],
    config['cookie']['expiry_days'],
    # api_key=os.environ.get('STREAMLIT_AUTH_API_KEY', None) # For 2FA/email features
)

# --- Login Widget ---
name, authentication_status, username = authenticator.login('Login', 'main')

if authentication_status == False:
    st.error('Username/password is incorrect')
elif authentication_status == None:
    st.warning('Please enter your username and password')
elif authentication_status:
    # --- Main App Content for Authenticated Users ---
    authenticator.logout('Logout', 'main')
    st.write(f'Welcome *{name}*')
    st.title('Application Content')
    st.write('This content is only visible to authenticated users.')

    # Example of accessing user info from session state
    # st.write(f"Current user: {st.session_state['username']}")
    # st.write(f"Authentication status: {st.session_state['authentication_status']}")

view raw JSON →