Skip to content

ares-coding/jwt-authentication-system-flask

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

3 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ” JWT Authentication System using Flask

A secure, production-ready token-based authentication backend built with Flask and JWT. This system implements industry-standard security practices for user registration, login validation, and protected API routes.

JWT Authentication Flask Python SQLite License

🌟 Features

  • πŸ”’ Secure User Registration - Password hashing with bcrypt
  • 🎫 Token-Based Authentication - JWT token generation and validation
  • πŸ›‘οΈ Protected API Routes - Bearer token authorization
  • πŸ‘€ User Login System - Credential validation and session management
  • πŸ’Ύ Database Persistence - SQLite with SQLAlchemy ORM
  • πŸš€ RESTful API - Clean and standardized endpoints

πŸ› οΈ Tech Stack

Backend

  • Python - Core programming language
  • Flask - Lightweight REST API framework

Authentication & Security

  • JWT (JSON Web Token) - Stateless token-based authentication
  • Flask-JWT / PyJWT - Token encoding and decoding
  • bcrypt / Flask-Bcrypt - Password hashing algorithm

Database

  • SQLite - Embedded database for user storage
  • SQLAlchemy - Object-Relational Mapping (ORM)

πŸ“‹ API Endpoints

Method Endpoint Description Authentication
POST /register Create new user account ❌ Public
POST /login Authenticate and get JWT token ❌ Public
GET /profile Get user profile information βœ… Required

πŸš€ Getting Started

Prerequisites

  • Python 3.8+
  • pip package manager

Installation

  1. Clone the repository
git clone https://github.com/ares-coding/jwt-authentication-flask.git
cd jwt-authentication-flask
  1. Create virtual environment
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate
  1. Install dependencies
pip install -r requirements.txt
  1. Set environment variables
export SECRET_KEY='your-secret-key-here'
export DATABASE_URL='sqlite:///users.db'
  1. Run the application
python app.py

The API will be available at http://localhost:5000

πŸ“ Usage Examples

1. Register a New User

curl -X POST http://localhost:5000/register \
  -H "Content-Type: application/json" \
  -d '{
    "username": "john_doe",
    "email": "john@example.com",
    "password": "SecurePass123!"
  }'

Response:

{
  "message": "User registered successfully",
  "user_id": 1
}

2. Login and Get JWT Token

curl -X POST http://localhost:5000/login \
  -H "Content-Type: application/json" \
  -d '{
    "username": "john_doe",
    "password": "SecurePass123!"
  }'

Response:

{
  "access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
  "token_type": "Bearer",
  "expires_in": 3600
}

3. Access Protected Route

curl -X GET http://localhost:5000/profile \
  -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."

Response:

{
  "user_id": 1,
  "username": "john_doe",
  "email": "john@example.com",
  "created_at": "2024-02-16T10:30:00Z"
}

πŸ”’ Security Features

  • Password Hashing: All passwords are hashed using bcrypt with salt rounds
  • JWT Tokens: Stateless authentication with configurable expiration
  • Bearer Token Authorization: Industry-standard HTTP authentication
  • SQL Injection Protection: SQLAlchemy ORM prevents SQL injection attacks
  • Input Validation: Server-side validation for all user inputs

πŸ“‚ Project Structure

jwt-authentication-flask/
β”œβ”€β”€ app.py                 # Main application file
β”œβ”€β”€ models.py              # Database models
β”œβ”€β”€ auth.py                # Authentication logic
β”œβ”€β”€ config.py              # Configuration settings
β”œβ”€β”€ requirements.txt       # Python dependencies
β”œβ”€β”€ README.md             # Project documentation
└── users.db              # SQLite database (generated)

πŸ§ͺ Testing

Run the test suite:

python -m pytest tests/

πŸ“¦ Dependencies

Flask==2.3.0
Flask-SQLAlchemy==3.0.0
Flask-Bcrypt==1.0.1
PyJWT==2.8.0
python-dotenv==1.0.0

🀝 Contributing

Contributions are welcome! Please follow these steps:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

πŸ“„ License

This project is licensed under the Apache License 2.0 - see the LICENSE file for details.

πŸ‘¨β€πŸ’» Author

Au.dev

Acknowledgments

  • Flask documentation and community
  • JWT.io for JWT standards
  • bcrypt library maintainers

⭐ Star this repository if you find it helpful!

Releases

No releases published

Packages

No packages published

Languages