Skip to main content
# app.py

from fastapi_auth.auth import setup_auth, build_auth_router, restrict, protect_router
from services.users import authenticate_user, create_user
from other_router import other_router
from fastapi import FastAPI
import uvicorn, os

# Configure auth library
auth_manager = setup_auth(
    auth_endpoint="/auth/login",
    jwt_secret_key=os.getenv("JWT_SECRET_KEY", "A-Secure-Key"),
    access_token_expire_minutes=int(os.getenv("ACCESS_TOKEN_EXPIRE_MINUTES", "1440")),
    token_renewal_minutes=int(os.getenv("TOKEN_RENEWAL_MINUTES", "30")),
    database_path=os.getenv("AUTH_DB_PATH", "auth.db") # Minimal SQLite for Auth/Tokens
)

app = FastAPI()

# Build and Register the Auth Router
app.include_router(
    build_auth_router(
        authenticate_user=authenticate_user,
        create_user=create_user
    )
)

@app.get("/restricted_ep")
@restrict(roles_allowed=["admin"], inject_user=True)
def root(current_user: dict):
    return {"message": f"Welcome {current_user['username']} - ID {current_user['user_id']}, to the API !"}

@app.get("/unrestricted_ep")
def root():
    return {"message": "Welcome to the API!"}

# Protect other routes
protect_router(other_router)
app.include_router(other_router)

if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=8000)
# services/users.py

from fastapi import HTTPException, status
from fastapi_auth.auth import get_auth_manager

def authenticate_user(username: str, password: str):
    # 1 - Get user from database by username, fetch password
    user = {} # dummy just for the example
    
    # 2 - Verify password using the manager helper fn
    if not get_auth_manager().verify_password(password, user["password"]):
        return None
    return {"user_id": username, "role": user["role"]}

def create_user(user_dict: dict):
    # 1 - Validate if username already exists
    user_already_exists = user_dict['username'] in [] # dummy just for the example
    if user_already_exists:
        raise HTTPException(
            status_code=status.HTTP_400_BAD_REQUEST,
            detail="Username already exists"
        )
    
    # 2 - Create user in database
    # user_dict contains username, password, role, and any extra metadata
    pass

Metadata

Release files for simple-fastapi-auth 1.1

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

Source distribution (sdist)

Source distribution for simple-fastapi-auth 1.1
File Size Uploaded Provenance
simple_fastapi_auth-1.1.tar.gz 8.9 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for simple-fastapi-auth 1.1
File Interpreter ABI Platform
simple_fastapi_auth-1.1-py3-none-any.whl Python 3 none any Details

Total release size: 17.4 kB

Release files / simple_fastapi_auth-1.1.tar.gz

Download URL simple_fastapi_auth-1.1.tar.gz
Size 8.9 kB
Tags Source
SHA-256 checksum
How to use checksums
07ac0a49c54e24244b1780e3c037c1ef59cf9ea19a3cc82a5efbbd2f129416a7
BLAKE2b-256 checksum
How to use checksums
95e1fe55d432a63d46e3386480195105300b62431dec8bd8f989c717dd94ae7c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.11.11

Release files / simple_fastapi_auth-1.1-py3-none-any.whl

Download URL simple_fastapi_auth-1.1-py3-none-any.whl
Size 8.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
194940717d7d0c76b609c89e4f903ba35e6d01e64d9eb015c8f7f31249a417af
BLAKE2b-256 checksum
How to use checksums
630514773901829e04893fc8d156a12c29ff6aa781b93955c745dd99abd65d2f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.11.11

Security for simple-fastapi-auth 1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.1 This release

2 release files No provenance

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