Skip to main content

A plug-and-play authentication system for FastAPI.

Project description

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}

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

fastauthx-1.1.0.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

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

FastAuthX-1.1.0-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file fastauthx-1.1.0.tar.gz.

File metadata

  • Download URL: fastauthx-1.1.0.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.3

File hashes

Hashes for fastauthx-1.1.0.tar.gz
Algorithm Hash digest
SHA256 ab386a0ae10b6bad76569f3aef023f5fffb1d989d5d8f14b2ac63f05dc7a1d1e
MD5 6eeaa4c5b66639bbac7ec40e66aeaff0
BLAKE2b-256 f5a300a9abbbc97e2662c7b281d7191fd0633b42d40ca481e384bfb4386f2f8e

See more details on using hashes here.

File details

Details for the file FastAuthX-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: FastAuthX-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.3

File hashes

Hashes for FastAuthX-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cb39b5f2e4e80b3403e359d3a2ad0982c3ebc464eec5bb895fb2291cb3c3593a
MD5 bdb016072fc914c7fd138ad5d7010482
BLAKE2b-256 b39f5b2da2c5b117fdeb577c2d9aa8cd2280472a83cba7165cd85dcbe39c0d6d

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