A tiny authentication toolkit for FastAPI.
Project description
AuthFast
A tiny authentication toolkit for FastAPI.
AuthFast gives FastAPI apps a simple way to add authentication without building the same routes, sessions, and database setup from scratch.
Install
pip install authfast
Default install supports SQLite. For production databases, install the matching driver extra:
pip install "authfast[postgres]"
pip install "authfast[mysql]"
pip install "authfast[all]"
Supported database targets:
- SQLite:
sqlite:///./app.dborsqlite+aiosqlite:///./app.db - PostgreSQL, Neon, Supabase Postgres:
postgresql://...orpostgresql+asyncpg://... - MySQL/MariaDB:
mysql://...ormysql+asyncmy://...
Create auth.py
Create .env next to auth.py:
AUTHFAST_SECRET_KEY=change-this-secret-for-local-development
from authfast import AuthFast
auth = AuthFast(
database="sqlite:///./app.db",
email_and_password={
"enabled": True,
},
)
Set Up The Database
authfast setup
The CLI looks for auth.py in ./, ./app, ./lib, ./src, ./src/app, and ./src/lib.
For SQLite, the relative database path is resolved from the directory where you run the command or server. Run authfast setup and uvicorn from the same project root.
Use a custom path:
authfast setup --config examples/basic/auth.py
Drop and recreate AuthFast tables:
authfast setup --force
Mount Routes
from fastapi import FastAPI
from auth import auth
app = FastAPI()
app.include_router(auth.router)
@app.get("/protected")
async def protected(user=auth.user()):
return {"id": user.id, "name": user.name, "email": user.email}
Use The API
Sign up:
curl -X POST http://127.0.0.1:8000/auth/sign-up/email \
-H "Content-Type: application/json" \
-d '{"name":"Dev","email":"dev@example.com","password":"secret123"}'
Log in:
curl -X POST http://127.0.0.1:8000/auth/sign-in/email \
-H "Content-Type: application/json" \
-d '{"email":"dev@example.com","password":"secret123"}'
Use the session cookie or returned bearer token:
curl http://127.0.0.1:8000/auth/me \
-H "Authorization: Bearer <access_token>"
Routes
POST /auth/sign-up/emailPOST /auth/sign-in/emailPOST /auth/sign-outPOST /auth/signupPOST /auth/loginPOST /auth/logoutGET /auth/meGET /auth/session
Example
From the project root:
authfast setup --config examples/basic/auth.py
uvicorn examples.basic.main:app --reload
Or from inside examples/basic:
authfast setup --config auth.py
uvicorn main:app --reload
Keep setup and uvicorn in the same working directory when using the example SQLite URL, because ./app.db is resolved from the current directory.
Reference
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 authfast-0.1.0.tar.gz.
File metadata
- Download URL: authfast-0.1.0.tar.gz
- Upload date:
- Size: 49.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a6fffcc64ea30db54af77f781c9b25c3d86138445d2ad39b33e9ab60f2f26409
|
|
| MD5 |
8d4ca25719249116659ec185122e8797
|
|
| BLAKE2b-256 |
ae0cf176fba8b4005198efa97c9e61e762b9fe638d625db8f176829f6290c249
|
File details
Details for the file authfast-0.1.0-py3-none-any.whl.
File metadata
- Download URL: authfast-0.1.0-py3-none-any.whl
- Upload date:
- Size: 14.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
79dd402a98c0777847a3cf4534c091b596f4078abd998ede8518e11bc4cadf3f
|
|
| MD5 |
b83218e52afa1d3e8bd2a573cc2796b0
|
|
| BLAKE2b-256 |
7a4962f069241e87ba37c3c9d7520d7abf24b6b0a933d089553acced8f4d9ae3
|