Skip to main content

FastAuthX

FastAuthX is a plug-and-play authentication package for FastAPI, designed to simplify and streamline the implementation of authentication with minimal setup. It supports token management, user validation, and password hashing, making it easy to integrate into your FastAPI applications.

Features

  • Simple and fast authentication for FastAPI
  • Automatic JWT token management (create, verify, and refresh)
  • Password hashing with bcrypt
  • Built-in OAuth2 password flow
  • Easy database integration with SQLAlchemy
  • Configurable and extensible (with a few lines of code)

Installation

To install FastAuthX, run the following command:

pip install FastAuthX


Usage
1. Initialize the authentication handler in your FastAPI app

from fastapi import FastAPI
from FastAuthX import AuthHandler

app = FastAPI()

# Initialize with your database URL
auth_handler = AuthHandler(DATABASE_URL="sqlite:///./database.db")


2. Use the AuthHandler to implement login and signup endpoints


from fastapi import Depends
from FastAuthX import schemas

@app.post("/login")
def login(request: schemas.Login, db: Session = Depends(get_db)):
    return auth_handler.login(request, db)

@app.post("/signup")
def signup(request: schemas.Signup, db: Session = Depends(get_db)):
    return auth_handler.signup(request, db)



3. Protect routes with authentication

from fastapi import Depends
from FastAuthX import AuthHandler, get_current_user

@app.get("/profile")
def profile(current_user: User = Depends(get_current_user)):
    return {"user": current_user}


Configuration
You can customize the authentication handler by passing the following arguments during initialization:

DATABASE_URL: Your database URL (e.g., "sqlite:///./database.db")
access_token_expire_min: Token expiration time in minutes (default: 15 minutes)
refresh_token_expire_day: Refresh token expiration time in days (default: 7 days)
ALGORITHM: JWT algorithm (default: HS256)
app_secret_key: The secret key for signing JWT tokens

Example
Here's a minimal example of a FastAPI application using FastAuthX:

from fastapi import FastAPI, Depends
from FastAuthX import AuthHandler, schemas

app = FastAPI()

# Initialize the authentication handler with your database URL
auth_handler = AuthHandler(DATABASE_URL="sqlite:///./database.db")

@app.post("/login")
def login(request: schemas.Login, db: Session = Depends(get_db)):
    return auth_handler.login(request, db)

@app.get("/profile")
def profile(current_user: User = Depends(get_current_user)):
    return {"user": current_user}

Metadata

Release files for FastAuthX 1.1.0

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

Source distribution (sdist)

Source distribution for FastAuthX 1.1.0
File Size Uploaded
fastauthx-1.1.0.tar.gz 4.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for FastAuthX 1.1.0
File Interpreter ABI Platform
FastAuthX-1.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 10.6 kB

Release files / fastauthx-1.1.0.tar.gz

Download URL fastauthx-1.1.0.tar.gz
Size 4.5 kB
Tags Source
SHA-256 checksum
How to use checksums
ab386a0ae10b6bad76569f3aef023f5fffb1d989d5d8f14b2ac63f05dc7a1d1e
BLAKE2b-256 checksum
How to use checksums
f5a300a9abbbc97e2662c7b281d7191fd0633b42d40ca481e384bfb4386f2f8e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.3

Release files / FastAuthX-1.1.0-py3-none-any.whl

Download URL FastAuthX-1.1.0-py3-none-any.whl
Size 6.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
cb39b5f2e4e80b3403e359d3a2ad0982c3ebc464eec5bb895fb2291cb3c3593a
BLAKE2b-256 checksum
How to use checksums
b39f5b2da2c5b117fdeb577c2d9aa8cd2280472a83cba7165cd85dcbe39c0d6d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.3

Release history Release notifications | RSS feed

This release

1.1.0 This release

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