Code Monkey home page Code Monkey logo

flask_crud's Introduction

JWT Auth Blog Project

This project demonstrates a simple blog application using Flask with JWT (JSON Web Token) authentication. It allows users to register, login, create, read, update, and delete blog posts.

Design Pattern

  1. Seperation of Concerns: This project is structed to sparate different functionalities into distinct modules such as auth module and blog_post module.
  2. Blueprints: Flask Blueprints are used to modularize the application. This allows different components of the app to be developed and tested independently, promoting modularity and reusability.
  3. SQLAlchemy: This project is used a high-level ORM to interact with the PostgreSQL database.
  4. JWT Authentication: Scalable authentication for RESTful APIs.
  5. Testing: Pytest and coverage test

Trade-Offs and Additional Improvements

  • User Roles and permissions: Implementing roles for admin, author and reader should be implemented.
  • Email Verification and Password Reset
  • Pagination and Filterin
  • Frontend Integration
  • Continuous Integration/Continuous Development(CI/CD)

Setup

Prerequisites

  • Python 3.x installed
  • PostgreSQL installed and running
  • Basic understanding of Flask and RESTful APIs

Installation

  1. Clone the repository:

    git clone https://github.com/your_username/jwt-auth-blog.git
    cd jwt-auth-blog
    
  2. Create and activate a virtual environment:

     python -m venv venv
     source venv/bin/activate      # On Windows use `venv\Scripts\activate`
    
  3. Install dependencies:

     pip install -r requirements.txt
    
  4. Set environment variables (optional, adjust as necessary):

     export FLASK_APP=run.py
     export FLASK_ENV=development
     export SECRET_KEY='your_secret_key'
     export JWT_SECRET_KEY='your_jwt_secret_key'
     export DATABASE_URL='postgresql://username:password@localhost/dbname'
    

Replace your_secret_key, your_jwt_secret_key, and postgresql://username:password@localhost/dbname with appropriate values.

  1. Initialize the database and run migrations:

     flask db init
     flask db migrate
     flask db upgrade
    

Running Tests

  1. Run unit tests
pytest

2. Run coverage tests
```bash
# Run tests with coverage
pytest --cov=app

# Generate a coverage report
coverage report -m

Usage

Running the Application

Start the Flask development server:

```bash
flask run

The application will be available at http://localhost:5000.

API Endpoints

Authentication

  • Register User:
    curl -X POST http://localhost:5000/auth/register \
     -H "Content-Type: application/json" \
     -d '{"email": "[email protected]", "password": "password123"}'
    
  • Login User:
    curl -X POST http://localhost:5000/auth/login \
     -H "Content-Type: application/json" \
     -d '{"email": "[email protected]", "password": "password123"}'
    

Blog Posts

  • Create Post:
    curl -X POST http://localhost:5000/posts \
     -H "Content-Type: application/json" \
     -H "Authorization: Bearer <YOUR_JWT_TOKEN>" \
     -d '{"title": "New Post", "body": "This is the body of the new post."}'
    
  • Get All Posts:
     curl http://localhost:5000/posts
    
  • Get Single Post:
    curl http://localhost:5000/posts
    
  • Update Post:
    curl -X PUT http://localhost:5000/posts/<post_id> \
     -H "Content-Type: application/json" \
     -H "Authorization: Bearer <YOUR_JWT_TOKEN>" \
     -d '{"title": "Updated Post Title", "body": "Updated body of the post."}'
    
  • Delete Post:
    curl -X DELETE http://localhost:5000/posts/<post_id> \
     -H "Authorization: Bearer <YOUR_JWT_TOKEN>"
    

Replace <YOUR_JWT_TOKEN> and <post_id> with actual values obtained from the login endpoint and respective post ID.

flask_crud's People

Contributors

intel-eth avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.