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.
- Seperation of Concerns: This project is structed to sparate different functionalities into distinct modules such as auth module and blog_post module.
- 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.
- SQLAlchemy: This project is used a high-level ORM to interact with the PostgreSQL database.
- JWT Authentication: Scalable authentication for RESTful APIs.
- Testing: Pytest and coverage test
- 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)
- Python 3.x installed
- PostgreSQL installed and running
- Basic understanding of Flask and RESTful APIs
-
Clone the repository:
git clone https://github.com/your_username/jwt-auth-blog.git cd jwt-auth-blog
-
Create and activate a virtual environment:
python -m venv venv source venv/bin/activate # On Windows use `venv\Scripts\activate`
-
Install dependencies:
pip install -r requirements.txt
-
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.
-
Initialize the database and run migrations:
flask db init flask db migrate flask db upgrade
- Run unit tests
pytest
2. Run coverage tests
```bash
# Run tests with coverage
pytest --cov=app
# Generate a coverage report
coverage report -m
Start the Flask development server:
```bash
flask run
The application will be available at http://localhost:5000.
- 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"}'
- 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.