🧠 Secure Flask-Based JSON Blog System
A lightweight, secure, file-based blog CMS built using Flask and Python.
Designed for simplicity, clarity, and full manual control over data persistence without a database engine.
📘 Project Overview
Blog CMS is a custom-built Flask-based blog management system developed as part of a roadmap.sh personal blog project. It allows a single administrator to create, edit, update, and delete blog posts through a secure authentication system.
Unlike traditional database-driven CMS platforms, Blog CMS stores all persistent data locally using structured JSON files inside the instance/ directory. This keeps the project lightweight, transparent, and easy to inspect without requiring external database services.
The system is built around Flask sessions, Werkzeug password hashing, and a custom route protection decorator to ensure that only authenticated administrators can access restricted functionality.
⚙️ Key Features
- Secure admin authentication system using Flask sessions
- Password hashing using Werkzeug security utilities
- Full CRUD functionality for blog posts (Create, Read, Update, Delete)
- JSON-based file storage (no database dependency)
- Protected admin routes using a custom decorator
- Dynamic post rendering using Jinja2 templates
- Modular helper-based architecture for separation of concerns
📂 Folder Structure
.
├── app.py
├── helpers/
│ ├── auth.py
│ └── posts.py
├── instance/
│ ├── admin.json
│ └── posts.json
├── static/
│ └── media/
├── templates/
│ ├── layout.html
│ ├── pages/
│ │ ├── home.html
│ │ ├── post.html
│ │ └── admin/
│ └── partials/
│ ├── header.html
│ └── footer.html
├── requirements.txt
├── .env
└── LICENSE
🚀 How to Run the Project
- Ensure Python 3.8+ is installed.
- Clone the repository:
git clone https://github.com/Sheikh-H/Blog-CMS.git
- Install dependencies:
pip install -r requirements.txt
- Create and activate a virtual environment (recommended).
- Run the Flask application:
python app.py
- Open your browser:
http://127.0.0.1:5000 OR localhost:5000
🔐 Admin Dashboard Access
To log in to the administrative control panel, you can use the default credentials provided below:
- Username: admin
- Password: password123
Alternatively, you can generate your own unique administrator account by navigating to the helpers/ directory and running the authentication script directly in your terminal:
python3 helpers/auth.py
🔐 Authentication System (helpers/auth.py)
login_required(f)
A Flask decorator that protects routes by checking whether admin_id exists in the session. If not present, the user is redirected to the admin login page.
Purpose: Prevent unauthorised access to administrative functionality such as creating, editing, or deleting posts.
login_function(username, password)
Handles admin authentication by loading credentials from admin.json and verifying password hashes using Werkzeug’s check_password_hash().
Returns: True if credentials are valid, otherwise False.
Error Handling: Safely handles missing files and invalid credentials without crashing the application.
register_user(username, password)
Creates a new admin user and stores the credentials securely inside admin.json. Passwords are hashed before being written to disk.
📝 Blog Post System (helpers/posts.py)
load_posts()
Loads all posts from posts.json. If the file does not exist, it is created and an empty list is returned.
load_post(_id)
Returns a single post matching the given ID or None if no match is found.
add_new_post(title, description, content, time)
Creates a new blog post, assigns an incremental ID, and stores it in JSON format.
delete_post(id)
Removes a post matching the given ID and updates the JSON file.
update_post(id, title=None, description=None, content=None)
Updates only the provided fields of a post while preserving existing values.
🌐 Flask Application Logic (app.py)
home()
Displays all blog posts on the homepage.
view_post(_id)
Displays a single blog post. Should ideally return a 404 if the post does not exist.
admin()
Handles admin login and session creation.
logout()
Clears the session and logs out the admin user.
add_post()
Creates a new blog post from form input and stores it via helper functions.
delete_posts(_id)
Deletes a blog post by ID and redirects to the dashboard.
edit_posts(_id)
Allows editing of an existing blog post.
dashboard()
Displays all posts in the admin control panel.
🔐 Security Model
- Werkzeug password hashing for secure credential storage
- Flask session-based authentication
- Route protection via custom
login_required decorator
- Admin-only access to all mutation routes
🧪 Error Handling Strategy
- Checks for missing JSON files and initialises them automatically
- Returns safe defaults instead of crashing (e.g. empty lists or None)
- Validates session state before accessing admin routes
- Uses controlled try/except blocks only where necessary
🧭 Planned Improvements
This project was built entirely independently without the use of AI-generated code or assistance.
Future improvements planned include:
- Migrating password hashing to Argon2 for stronger modern cryptographic security
- Replacing manual form handling with Flask-WTF for validation and CSRF protection
- Adding full CSRF protection across all admin routes
- Improving input sanitisation and validation
📦 Environment Variables (.env)
The .env file is included in this project to store the Flask secret key required for session management. It is provided intentionally to make setup easier for anyone running the project locally, so users do not need to manually generate their own secret key before launching the application.
📄 Licence
This project is licensed under the MIT Licence — see the LICENCE file for details.
MIT Licence
Copyright (c) 2026 Sheikh Hussain
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Footnote
🗣️ Feel free to follow, connect, and chat!
⭐ If you like this project, please give it a star on GitHub!