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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fastauthx-1.0.0.tar.gz.
File metadata
- Download URL: fastauthx-1.0.0.tar.gz
- Upload date:
- Size: 2.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6c91581f5720a20fb61cd0158d138399c1ea82441503cef74dc3afa40457f749
|
|
| MD5 |
5860d21e3a784f008df8cfce6a34ac66
|
|
| BLAKE2b-256 |
07a8370f394fae182e85a0220f44173b9b76136f4fa233f21fe99df15e150cee
|
File details
Details for the file FastAuthX-1.0.0-py3-none-any.whl.
File metadata
- Download URL: FastAuthX-1.0.0-py3-none-any.whl
- Upload date:
- Size: 2.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c05e056be10cd05d88eba1a2265ee96f7ff0c427c32c665e68ca1a33c6713039
|
|
| MD5 |
0285feb67c6a47fffe990d3be3efc7c4
|
|
| BLAKE2b-256 |
dbedadd51824c35eb58bded0f8ccd6cd6958e00d8bee893fd0e9ab9e93567c82
|