Files
Redflag/docs/4_LOG/December_2025/2025-12-15_Admin_Login_Fix.md

3.1 KiB

RedFlag Admin Login Fix - COMPLETED ✓

Final Status: SUCCESS

Login now works! The admin can successfully authenticate and receive a JWT token.

Root Cause

The Admin struct had ID int64 but the database uses UUID type, causing a type mismatch during SQL scanning which prevented proper password verification.

What Was Fixed

1. Column name mismatches in admin.go

Fixed all SQL queries to match the database schema (migration 001):

  • CreateAdminIfNotExists: Removed non-existent updated_at column from INSERT
  • UpdateAdminPassword: Changed passwordpassword_hash, removed updated_at
  • VerifyAdminCredentials: Changed passwordpassword_hash, removed updated_at
  • GetAdminByUsername: Removed updated_at from SELECT

2. Type mismatch in Admin struct

  • Changed ID field from int64 to uuid.UUID to match database
  • Added github.com/google/uuid import
  • Removed UpdatedAt field (doesn't exist in database)

3. Execution order fix

  • Admin creation now happens AFTER isSetupComplete() validation
  • Prevents creating admin with incomplete configuration

4. Docker-compose fix

  • Removed hardcoded postgres credentials that were overriding .env values

Testing Results

$ curl -X POST http://localhost:8080/api/v1/auth/login \
  -H "Content-Type: application/json" \
  -d '{"username":"admin","password":"Qu@ntum21!"}'

Response:
{
  "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
  "user": {
    "id": "b0ea99d0-e3ce-40cd-a510-1fb56072646a",
    "username": "admin",
    "email": "",
    "created_at": "2025-12-15T03:10:53.38145Z"
  }
}
HTTP Status: 200

What to Test Next

  1. Use the JWT token to access protected endpoints:

    curl -H "Authorization: Bearer <token>" http://localhost:8080/api/v1/stats/summary
    
  2. Verify the web dashboard loads and works with the token

  3. Test admin password sync: Change password in config/.env and restart to verify it updates

Quick Reference Commands

# View logs
docker compose logs server --tail=50

# Stream logs
docker compose logs server -f

# Check database
docker compose exec postgres psql -U redflag -d redflag -c "SELECT * FROM users;"

# Test login
curl -X POST http://localhost:8080/api/v1/auth/login \
  -H "Content-Type: application/json" \
  -d '{"username":"admin","password":"Qu@ntum21!"}'

# Restart after code changes
docker compose build server && docker compose up -d --force-recreate server

# Full restart (if needed)
docker compose down && docker compose up -d

Files Modified

  • aggregator-server/internal/database/queries/admin.go - Fixed SQL queries and Admin struct
  • docker-compose.yml - Removed hardcoded postgres credentials

Current Database Schema (users table)

id              UUID PRIMARY KEY
db_username     VARCHAR(255) UNIQUE
email           VARCHAR(255) UNIQUE
password_hash   VARCHAR(255)
role            VARCHAR(50)
created_at      TIMESTAMP
last_login      TIMESTAMP

Notes

  • The .env has two REDFLAG_SIGNING_PRIVATE_KEY entries (second overwrites first)
  • Admin creation only runs when all setup validation passes
  • Password is synced from .env on every startup (UpdateAdminPassword function)