Skip to content

Conversation

@codesungrape
Copy link
Collaborator

Description

This PR adds /auth/login endpoint (PyJWT) to issue tokens after password verification and integrates Flask-Bcrypt for password hashing/verification.
It also adds a require_jwt decorator that validates Authorization: Bearer , and attaches user.

Please delete options that are not relevant.

  • New feature (non-breaking change which adds functionality)
  • This change requires a documentation update - HAVEN'T DONE YET
  • Code refactor (improving code quality without changing functionality)

How Has This Been Tested?

Automated test suite in tests/test_auth.py
Manual Testing with cURL and mongosh
CI/CD

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • My individual commit messages are descriptive and follow our commit guidelines
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR implements JWT-based authentication by adding a /auth/login endpoint and a require_jwt decorator for protecting routes. The changes integrate Flask-Bcrypt for password hashing and PyJWT for token generation/validation, replacing the previous basic bcrypt implementation.

  • Adds JWT authentication with login endpoint and route protection decorator
  • Migrates from raw bcrypt to Flask-Bcrypt for consistent password hashing
  • Updates database schema to use password field instead of password_hash

Reviewed Changes

Copilot reviewed 9 out of 10 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
app/extensions.py Adds Flask-Bcrypt extension initialization
app/init.py Initializes bcrypt extension in application factory
app/routes/auth_routes.py Adds login endpoint with JWT token generation and migrates to Flask-Bcrypt
app/utils/decorators.py Implements JWT validation decorator for protecting routes
scripts/seed_users.py Updates user seeding to use new password field name
tests/conftest.py Adds test fixtures for seeded user data with hashed passwords
tests/test_auth.py Adds comprehensive tests for login endpoint functionality
tests/test_decorators.py Adds thorough test coverage for JWT decorator validation
tests/scripts/test_seed_users.py Updates test to use new password field name

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

"""
Test suite for decorators
To test the decorator in isolation, we'll create a tiny, temporary Flask app inside out test file.
Copy link

Copilot AI Aug 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo: 'out' should be 'our'

Suggested change
To test the decorator in isolation, we'll create a tiny, temporary Flask app inside out test file.
To test the decorator in isolation, we'll create a tiny, temporary Flask app inside our test file.

Copilot uses AI. Check for mistakes.
assert data["error"] == expected_message


def test_loginhandles_jwp_encoding_error(client, seeded_user_in_db):
Copy link

Copilot AI Aug 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function name has typos: 'loginhandles' should be 'login_handles' and 'jwp' should be 'jwt'

Suggested change
def test_loginhandles_jwp_encoding_error(client, seeded_user_in_db):
def test_login_handles_jwt_encoding_error(client, seeded_user_in_db):

Copilot uses AI. Check for mistakes.
}
# Patch jwt.encode() to be a mock
with patch("app.routes.auth_routes.jwt.encode") as mock_jwt_encode:
# Configure the mosk to raise the specific exception we want to test
Copy link

Copilot AI Aug 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo: 'mosk' should be 'mock'

Suggested change
# Configure the mosk to raise the specific exception we want to test
# Configure the mock to raise the specific exception we want to test

Copilot uses AI. Check for mistakes.
@pytest.fixture(scope="session") # because this data never changes
def mock_user_data():
"""Provides a dictionary of a test user's data, with a hashed password."""
# USe Flask-Bcrypt's fucntion to CREATE the hash.
Copy link

Copilot AI Aug 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment has typos: 'USe' should be 'Use' and 'fucntion' should be 'function'

Suggested change
# USe Flask-Bcrypt's fucntion to CREATE the hash.
# Use Flask-Bcrypt's function to CREATE the hash.

Copilot uses AI. Check for mistakes.
@@ -0,0 +1,65 @@
# pylint: disable=too-many-return-statements
"""..."""
Copy link

Copilot AI Aug 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Module docstring is incomplete - should describe the purpose of the decorators module

Suggested change
"""..."""
"""
This module provides decorators for Flask routes, including JWT authentication.
The `require_jwt` decorator protects routes by verifying JWT tokens in the
'Authorization: Bearer <token>' header, decoding and validating the token,
and attaching the authenticated user to the request context.
"""

Copilot uses AI. Check for mistakes.
import jwt
from email_validator import EmailNotValidError, validate_email
from flask import Blueprint, jsonify, request
from flask import Blueprint, current_app, jsonify, request
Copy link

Copilot AI Aug 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Import organization: datetime and jwt imports should be grouped together as they are both third-party libraries

Copilot uses AI. Check for mistakes.
@codesungrape codesungrape merged commit 16337b4 into main Aug 18, 2025
2 checks passed
@codesungrape codesungrape deleted the Add-/auth/register-endpoint-for-JWT-securitsation branch August 19, 2025 08:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants