Skip to content

Showcase of Python REST API server with JWT Authentication and Memory Caching using FastAPI framework

Notifications You must be signed in to change notification settings

s101d1/fastapi-sqlalchemy-jwt-auth-showcase

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

FastAPI + SQLAlchemy 2.0 + MySQL + JWT Authentication + Memory Caching

A demonstration of a Python REST API server with JWT Authentication and Memory Caching using FastAPI framework.

Database connection is handled by SQLAlchemy 2.0.x library.

JWT authentication is handled by python-jose library.

The caching is handled by cachetools library.

The payload size limiter is handled by content-size-limit-asgi middleware library.

Getting Started

Requirements

  • Python v3.10.x or newer.
  • MySQL database v8.0 or newer with an empty database/schema prepared.

How to install the app

  • Initialize and activate virtual environment inside the project folder:

    $ python3 -m venv venv
    $ . venv/bin/activate
  • Install the required libraries:

    $ pip3 install -r requirements.txt
  • Modify the DB_URL environment variable in .env file according to your database.

How to run the app

  • Activate the virtual environment if you haven't already:

    $ . venv/bin/activate
  • Run the server:

    $ uvicorn main:app --reload

    The server will run at http://localhost:8000

    The swagger API docs can be accessed at http://localhost:8000/docs

    The tables in database will be created automatically if they don't exist yet when the server starts or reloaded.

API Endpoints Information

  1. POST /auth/signup (Signup endpoint)

    • Accepts email, password, and confirm_password values.
    • Returns a token (JWT) with 1 hour expiry time if successful.
  2. POST /auth/login (Login endpoint)

    • Accepts email and password values.
    • Returns a token (JWT) with 1 hour expiry time if successful.
  3. POST /posts (Add New Post endpoint)

    • Endpoint is protected by JWT authentication (Bearer <token> header is required).
    • Accepts text value, and it will create a new Post that belongs to the authenticated User if successful.
    • Payload size is limited to 1 MB.
  4. GET /posts (Get Posts endpoint)

    • Endpoint is protected by JWT authentication (Bearer <token> header is required).
    • Returns all user's posts.
    • Successful response is cached for 5 minutes for the authenticated user. Cache will be invalidated if user adds a new post or deletes a post.
  5. DELETE /posts/:id (Delete Post endpoint)

    • Endpoint is protected by JWT authentication (Bearer <token> header is required).
    • Deletes a Post by the id that belongs to the authenticated User.

The API endpoint's request parameter and response body details can be seen in the swagger API docs (http://localhost:8000/docs)

About

Showcase of Python REST API server with JWT Authentication and Memory Caching using FastAPI framework

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages