PropelAuth Python SDK

4.3.2 · active · verified Thu Apr 16

PropelAuth is a Python library for managing authentication and authorization in B2B/multi-tenant applications. It provides features like user login, signup, organization management, roles, permissions, and API key authentication. The library simplifies backend authorization with hosted UIs and a developer-friendly SDK. It maintains an active release cadence with frequent updates for new features and improvements. Current version is 4.3.2.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the PropelAuth client and validate an access token. It uses environment variables for secure configuration. Upon successful validation, it prints user and organization details. It also includes commented-out code for an example backend API call (e.g., creating a magic link).

import os
from propelauth_py import init_base_auth, UnauthorizedException

# Your PropelAuth Auth URL and API Key from your PropelAuth dashboard
AUTH_URL = os.environ.get('PROPELAUTH_AUTH_URL', 'YOUR_AUTH_URL')
API_KEY = os.environ.get('PROPELAUTH_API_KEY', 'YOUR_API_KEY')

if not AUTH_URL or AUTH_URL == 'YOUR_AUTH_URL' or not API_KEY or API_KEY == 'YOUR_API_KEY':
    print("Please set PROPELAUTH_AUTH_URL and PROPELAUTH_API_KEY environment variables")
    exit(1)

try:
    auth = init_base_auth(AUTH_URL, API_KEY)
    
    # Simulate an Authorization header from an incoming request
    # In a real application, this would come from a client request
    mock_auth_header = "Bearer a_mock_jwt_token"
    
    # Validate the access token and get user information
    user = auth.validate_access_token_and_get_user(mock_auth_header)
    
    print(f"Successfully authenticated user: {user.user_id}")
    print(f"User email: {user.email}")
    if user.orgs:
        print("User belongs to organizations:")
        for org_member_info in user.orgs:
            print(f"  - Org ID: {org_member_info.org_id}, Name: {org_member_info.org_name}, Roles: {org_member_info.roles}")
            
    # Example of calling a backend API to create a magic link
    # Note: This requires appropriate permissions on your API key
    # magic_link_response = auth.create_magic_link("test@example.com")
    # print(f"Magic link URL: {magic_link_link.url}")

except UnauthorizedException:
    print("Authentication failed: Invalid access token or configuration.")
except Exception as e:
    print(f"An error occurred: {e}")

view raw JSON →