Skip to content

Flask User Authentication with JSON Web Token is a Python-based application that implements user authentication using Flask and JSON Web Tokens (JWT). It provides endpoints for user registration and login, and secures routes using JWT-based authentication.

Notifications You must be signed in to change notification settings

sorXCode/flask_user_auth_with_token

Repository files navigation

Flask User Authentication with JSON Web Token

Overview

This project is a user authentication system built using Flask and JSON Web Tokens (JWT). It provides secure authentication for web applications, enabling user registration, login, and token-based access to protected routes.

Features

  • User Registration – Allows users to create accounts.
  • User Login – Secure authentication using JWT.
  • Token-Based Authentication – Provides protected endpoints.
  • Password Hashing – Uses industry-standard security practices.
  • Role-Based Access Control (Optional) – Restrict access based on user roles.

Tech Stack

  • Flask – Lightweight Python web framework.
  • Flask-JWT-Extended – Secure JWT-based authentication.
  • Flask-SQLAlchemy – ORM for database management.
  • Flask-Migrate – Database migrations with Alembic.
  • SQLite/PostgreSQL – Database options.

Installation

Clone the repository:

git clone https://github.com/sorXCode/flask_user_auth_with_token.git
cd flask_user_auth_with_token

Create a virtual environment:

python -m venv venv
source venv/bin/activate  # On Windows use `venv\Scripts\activate`

Install dependencies:

pip install -r requirements.txt

Configuration

Update the .env file with your database URL and secret key:

SECRET_KEY=your_secret_key
DATABASE_URL=sqlite:///site.db  # Change for PostgreSQL if needed

Running the Application

Initialize the database:

flask db upgrade

Run the Flask application:

flask run

API Endpoints

User Registration

POST /register

Request:

{
  "username": "example_user",
  "password": "securepassword"
}

User Login

POST /login

Request:

{
  "username": "example_user",
  "password": "securepassword"
}

Response:

{
  "access_token": "your_jwt_token"
}

Protected Route

GET /protected

Requires an Authorization header:

Authorization: Bearer your_jwt_token

License

This project is licensed under the MIT License.


🚀 Secure your Flask app with JWT authentication today!

About

Flask User Authentication with JSON Web Token is a Python-based application that implements user authentication using Flask and JSON Web Tokens (JWT). It provides endpoints for user registration and login, and secures routes using JWT-based authentication.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages