Skip to content

Refactor user and auth logic into dedicated service layer#38

Merged
zlendo1 merged 4 commits into
developmentfrom
copilot/refactor-user-auth-service
Oct 28, 2025
Merged

Refactor user and auth logic into dedicated service layer#38
zlendo1 merged 4 commits into
developmentfrom
copilot/refactor-user-auth-service

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Oct 28, 2025

Extract authentication and user management logic from controllers into dedicated service classes following existing service patterns (ai_agents_service, routing_service).

Changes

New Services

  • app/services/auth_service.py - Password hashing/verification, user authentication, token generation, JWT validation
  • app/services/user_service.py - User CRUD operations, validation (username/password length, uniqueness checks)

Refactored

  • app/core/security.py - Reduced to JWT token creation and OAuth2 scheme definition only
  • app/api/v1/endpoints/auth.py - Removed direct DB queries and validation; delegates to services
  • app/api/v1/endpoints/users.py - Uses auth_service.get_current_user instead of core.security

Before/After

# Before: Controller handled everything
@router.post("/register")
async def register_user(user_in: UserCreate, db: Session = Depends(get_db)):
    user = db.query(User).filter(User.username == user_in.username).first()
    if user:
        raise HTTPException(400, "User exists")
    # ... validation, hashing, token generation inline

# After: Delegated to services  
@router.post("/register")
async def register_user(user_in: UserCreate, db: Session = Depends(get_db)):
    hashed_password = auth_service.get_password_hash(user_in.password)
    user = user_service.create_user(db, user_in, hashed_password)
    return {"access_token": auth_service.generate_access_token(user.id), ...}

All tests updated to import from service layer. No breaking changes to API contracts.

Original prompt

This section details on the original issue you should resolve

<issue_title>Refactor the user service</issue_title>
<issue_description>- Separate the user service from the controller

  • Seperate the auth service from the controller
  • Move core.security to a service concerning users authentificatoin
  • (Potentially) adjust tests for change in structure</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 2 commits October 28, 2025 13:47
…lers

Co-authored-by: zlendo1 <115471708+zlendo1@users.noreply.github.com>
… user_service in auth

Co-authored-by: zlendo1 <115471708+zlendo1@users.noreply.github.com>
Copilot AI changed the title [WIP] Refactor user service and authentication handling Refactor user and auth logic into dedicated service layer Oct 28, 2025
Copilot AI requested a review from zlendo1 October 28, 2025 13:54
@zlendo1 zlendo1 marked this pull request as ready for review October 28, 2025 14:01
@zlendo1 zlendo1 merged commit 91d21d6 into development Oct 28, 2025
1 check passed
@zlendo1 zlendo1 mentioned this pull request Oct 28, 2025
@zlendo1 zlendo1 deleted the copilot/refactor-user-auth-service branch October 30, 2025 12:14
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.

2 participants