Skip to main content

Streamlit Google Auth

This package provides a simple and easy-to-use integration of Google authentication in your Streamlit application.

Getting Started

  1. Install the package:
    pip install streamlit-google-auth

  2. Create a Google Cloud Platform project and obtain the client ID and client secret. You can follow the instructions here to create the credentials.

  3. Save the client ID and client secret in a JSON file (e.g., google_credentials.json).

  4. Import the necessary modules and initialize the Authenticate class:

    import streamlit as st
    from streamlit_google_auth import Authenticate
    
    authenticator = Authenticate(
        secret_credentials_path='google_credentials.json',
        cookie_name='my_cookie_name',
        cookie_key='this_is_secret',
        redirect_uri='http://localhost:8501',
    )
    
  5. Check if the user is already authenticated and handle the login/logout flow:

    # Check if the user is already authenticated
    authenticator.check_authentification()
    
    # Display the login button if the user is not authenticated
    if not st.session_state.get('connected', False):
        authorization_url = authenticator.get_authorization_url()
        st.markdown(f'[Login]({authorization_url})')
        st.link_button('Login', authorization_url)
    # Display the user information and logout button if the user is authenticated
    else:
        st.image(st.session_state['user_info'].get('picture'))
        st.write(f"Hello, {st.session_state['user_info'].get('name')}")
        st.write(f"Your email is {st.session_state['user_info'].get('email')}")
        if st.button('Log out'):
            authenticator.logout()
    

That's it! Your Streamlit app now has Google authentication integrated.

Configuration

The Authenticate class takes the following parameters:

  • secret_credentials_path: The path to the Google Cloud Platform credentials JSON file.
  • cookie_name: The name of the cookie used to store the authentication information.
  • cookie_key: The secret key used to encrypt the cookie.
  • redirect_uri: The redirect URI of your Streamlit application.
  • cookie_expiry_days: Optional, The number of days the cookie stay valid.

Functions

The Authenticate class provides the following functions:

  • check_authentification(): Catch the event when the user come back from the google login page and log it.
  • login(): Displays the login button and handles the authentication flow.
  • logout(): Logs out the user and clears the session state.
  • get_authorization_url(): Returns the URL for the Google authentication page.

Session State

The Authenticate class updates the following keys in the Streamlit session state:

  • connected: A boolean indicating whether the user is authenticated or not.
  • oauth_id: The unique identifier for the authenticated user.
  • user_info: A dictionary containing the user's name, email, and profile picture URL.

Example

Here's a complete example of how to use the Authenticate class in a Streamlit app:

import streamlit as st
from streamlit_google_auth import Authenticate

st.title('Streamlit Google Auth Example')

if 'connected' not in st.session_state:
    authenticator = Authenticate(
        secret_credentials_path = 'google_credentials.json',
        cookie_name='my_cookie_name',
        cookie_key='this_is_secret',
        redirect_uri = 'http://localhost:8501',
    )
    st.session_state["authenticator"] = authenticator

# Catch the login event
st.session_state["authenticator"].check_authentification()

# Create the login button
st.session_state["authenticator"].login()

if st.session_state['connected']:
    st.image(st.session_state['user_info'].get('picture'))
    st.write('Hello, '+ st.session_state['user_info'].get('name'))
    st.write('Your email is '+ st.session_state['user_info'].get('email'))
    if st.button('Log out'):
        st.session_state["authenticator"].logout()

Metadata

Release files for streamlit-google-auth 1.1.8

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for streamlit-google-auth 1.1.8
File Size Uploaded
streamlit-google-auth-1.1.8.tar.gz 7.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for streamlit-google-auth 1.1.8
File Interpreter ABI Platform
streamlit_google_auth-1.1.8-py3-none-any.whl Python 3 none any Details

Total release size: 14.4 kB

Release files / streamlit-google-auth-1.1.8.tar.gz

Download URL streamlit-google-auth-1.1.8.tar.gz
Size 7.3 kB
Tags Source
SHA-256 checksum
How to use checksums
a844b07933d5e95b880bd72753b1ac9e07c8b62210ff5e3e3e69d48a3826883f
BLAKE2b-256 checksum
How to use checksums
96237fdca3d0cfe49892331c0fb957b06ce332324faed03988856da00b1ea1cf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.11.5

Release files / streamlit_google_auth-1.1.8-py3-none-any.whl

Download URL streamlit_google_auth-1.1.8-py3-none-any.whl
Size 7.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5b3db4c769fcc51469046af85fd771c138e02629312dc58877bb8bbca91e9a83
BLAKE2b-256 checksum
How to use checksums
daa6c530c084bfff54ce185b1e13386ccff43c610f6a2511638dbd2c7db0d99f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.11.5

Release history Release notifications | RSS feed

This release

1.1.8 This release

2 release files

1.1.7

2 release files

1.1.6

2 release files

1.1.5

2 release files

1.1.4

2 release files

1.1.3

2 release files

1.1.2

2 release files

1.1.1

2 release files

1.1

2 release files

1.0.4

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page