Skip to main content

Use Google authentification in streamlit

Project description

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()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

streamlit-google-auth-1.1.4.tar.gz (7.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

streamlit_google_auth-1.1.4-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

Details for the file streamlit-google-auth-1.1.4.tar.gz.

File metadata

  • Download URL: streamlit-google-auth-1.1.4.tar.gz
  • Upload date:
  • Size: 7.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.11.5

File hashes

Hashes for streamlit-google-auth-1.1.4.tar.gz
Algorithm Hash digest
SHA256 d081c4720aaa2ec5efa4462241816a5c85e612b65a15f0bf0d295f98da806418
MD5 0b326a90c42c060d7ad07448ca47aa56
BLAKE2b-256 179f20c2a563b05146f7c4190ec3f2ed935dfc210421809229dbcdfecb237095

See more details on using hashes here.

File details

Details for the file streamlit_google_auth-1.1.4-py3-none-any.whl.

File metadata

File hashes

Hashes for streamlit_google_auth-1.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 b4c6cd1fc06719c4075987b3977d022009efd52f48c1e049c73bf024739283bc
MD5 6b959fd608aa0b463aad61bafef7b700
BLAKE2b-256 fd8b05f0ce86aa806b39afb98786e77954922e1eea0eb96e3c48c45773b2e14f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page