Skip to main content

Python Client Library for Supabase Auth

Project description

Auth-py

CI Python Version

This is a Python port of the supabase js gotrue client. The current state is that there is a features parity but with small differences that are mentioned in the section Differences to the JS client. As of December 14th, we renamed to repo from gotrue-py to auth-py to mirror the changes in the JavaScript library.

Installation

The package can be installed using pip, uv or poetry:

Pip

pip install supabase_auth

Uv

uv add supabase_auth

Poetry

poetry add supabase_auth

Features

  • Full feature parity with the JavaScript client
  • Support for both synchronous and asynchronous operations
  • MFA (Multi-Factor Authentication) support
  • OAuth and SSO integration
  • Magic link and OTP authentication
  • Phone number authentication
  • Anonymous sign-in
  • Session management with auto-refresh
  • JWT token handling and verification
  • User management and profile updates

Differences to the JS client

It should be noted there are differences to the JS client. If you feel particulaly strongly about them and want to motivate a change, feel free to make a GitHub issue and we can discuss it there.

Firstly, feature pairity is not 100% with the JS client. In most cases we match the methods and attributes of the JS client and api classes, but is some places (e.g for browser specific code) it didn't make sense to port the code line for line.

There is also a divergence in terms of how errors are raised. In the JS client, the errors are returned as part of the object, which the user can choose to process in whatever way they see fit. In this Python client, we raise the errors directly where they originate, as it was felt this was more Pythonic and adhered to the idioms of the language more directly.

In JS we return the error, but in Python we just raise it.

const { data, error } = client.sign_up(...)

The other key difference is we do not use pascalCase to encode variable and method names. Instead we use the snake_case convention adopted in the Python language.

Also, the supabase_auth library for Python parses the date-time string into datetime Python objects. The JS client keeps the date-time as strings.

Usage

The library provides both synchronous and asynchronous clients. Here are some examples:

Synchronous Client

from supabase_auth import SyncGoTrueClient

headers = {
    "apiKey": "my-mega-awesome-api-key",
    # ... any other headers you might need.
}
client: SyncGoTrueClient = SyncGoTrueClient(url="www.genericauthwebsite.com", headers=headers)

# Sign up with email and password
user = client.sign_up(email="example@gmail.com", password="*********")

# Sign in with email and password
user = client.sign_in_with_password(email="example@gmail.com", password="*********")

# Sign in with magic link
user = client.sign_in_with_otp(email="example@gmail.com")

# Sign in with phone number
user = client.sign_in_with_otp(phone="+1234567890")

# Sign in with OAuth
user = client.sign_in_with_oauth(provider="google")

# Sign out
client.sign_out()

# Get current user
user = client.get_user()

# Update user profile
user = client.update_user({"data": {"name": "John Doe"}})

Asynchronous Client

from supabase_auth import AsyncGoTrueClient

headers = {
    "apiKey": "my-mega-awesome-api-key",
    # ... any other headers you might need.
}
client: AsyncGoTrueClient = AsyncGoTrueClient(url="www.genericauthwebsite.com", headers=headers)

async def main():
    # Sign up with email and password
    user = await client.sign_up(email="example@gmail.com", password="*********")

    # Sign in with email and password
    user = await client.sign_in_with_password(email="example@gmail.com", password="*********")

    # Sign in with magic link
    user = await client.sign_in_with_otp(email="example@gmail.com")

    # Sign in with phone number
    user = await client.sign_in_with_otp(phone="+1234567890")

    # Sign in with OAuth
    user = await client.sign_in_with_oauth(provider="google")

    # Sign out
    await client.sign_out()

    # Get current user
    user = await client.get_user()

    # Update user profile
    user = await client.update_user({"data": {"name": "John Doe"}})

# Run the async code
import asyncio
asyncio.run(main())

MFA Support

The library includes support for Multi-Factor Authentication:

# List MFA factors
factors = client.mfa.list_factors()

# Enroll a new MFA factor
enrolled_factor = client.mfa.enroll({"factor_type": "totp"})

# Challenge and verify MFA
challenge = client.mfa.challenge({"factor_id": "factor_id"})
verified = client.mfa.verify({"factor_id": "factor_id", "code": "123456"})

# Unenroll a factor
client.mfa.unenroll({"factor_id": "factor_id"})

Contributions

We would be immensely grateful for any contributions to this project.

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

supabase_auth-2.27.3.tar.gz (39.2 kB view details)

Uploaded Source

Built Distribution

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

supabase_auth-2.27.3-py3-none-any.whl (48.5 kB view details)

Uploaded Python 3

File details

Details for the file supabase_auth-2.27.3.tar.gz.

File metadata

  • Download URL: supabase_auth-2.27.3.tar.gz
  • Upload date:
  • Size: 39.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.8.2

File hashes

Hashes for supabase_auth-2.27.3.tar.gz
Algorithm Hash digest
SHA256 39894d4bc60b6f23b5cff4d0d7d4c1659e5d69563cadf014d4896f780ca8ca78
MD5 b1e6e1fe3d9b01027469a74f9544b574
BLAKE2b-256 efd3e1ba366673f74920cdb2d41a427375ac1f8c422d35ba09b63f9f86fece59

See more details on using hashes here.

File details

Details for the file supabase_auth-2.27.3-py3-none-any.whl.

File metadata

File hashes

Hashes for supabase_auth-2.27.3-py3-none-any.whl
Algorithm Hash digest
SHA256 82a4262eaad85383319d394dab0eea11fcf3ebd774062aef8ea3874ae2f02579
MD5 268410fde4ef85a667c96e8655365cb0
BLAKE2b-256 a7135e294b2374ee2aa459320f754f7f332b828d1511eda09248957c65123dcb

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