Skip to content

LaxmanMaharjan/fastapi-auth-middlewares

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

FastAPI Auth Middlewares

FastAPI Auth Middlewares is a package for securing FastAPI application. It provides a convenient way to secure your FastAPI routes and endpoints using jwt tokens.

Features

  • Seamless integration with FastAPI applications.
  • Easily protect routes and endpoints with JWT authentication.
  • Lightweight and designed for simplicity.

Requirements

Python 3.8

Installation

$ pip install fastapi_auth_middlewares

Example

Create it

  • Create a file main.py with:
from fastapi import FastAPI, Request

from fastapi_auth_middlewares import JwtAuthMiddleware

app = FastAPI(
    title="Secured Project",
    version="1.0",
)


app.add_middleware(
    JwtAuthMiddleware,
    secret_key="your_secret_key",
    algorithms=["HS256"],
    # Excluding Documentation (OpenAPI and favicon routes) and health check routes from authentication
    public_paths=["/docs", "/favicon.ico", "/openapi.json", "/api/health"],
)


@app.get("/api/health")
async def health():
    return {"message": "server is up and running."}, 200


@app.get("/protected")
async def protected_route(request: Request):
    # Access the decoded_token from the middleware
    decoded_token = request.state.user

    # Your logic using the decoded_token
    user_id = decoded_token.get("user_id")
    username = decoded_token.get("username")

    return {
        "message": "This is a protected route",
        "user_id": user_id,
        "username": username,
    }

Run it

  • Run the server with:
$ uvicorn main:app --reload

INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO:     Started reloader process [28720]
INFO:     Started server process [28722]
INFO:     Waiting for application startup.
INFO:     Application startup complete.

Contributing

Feel free to contribute to this project.

License

This project is licensed under the terms of the MIT license.

About

A middleware for securing fastapi application.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages