Files
PROJECT-CONTORL/openspec/changes/archive/2025-12-28-add-user-auth/tasks.md
beabigegg 1fda7da2c2 feat: implement user authentication module
- Backend (FastAPI):
  - External API authentication (pj-auth-api.vercel.app)
  - JWT token validation with Redis session storage
  - RBAC with department isolation
  - User, Role, Department models with pjctrl_ prefix
  - Alembic migrations with project-specific version table
  - Complete test coverage (13 tests)

- Frontend (React + Vite):
  - AuthContext for state management
  - Login page with error handling
  - Protected route component
  - Dashboard with user info display

- OpenSpec:
  - 7 capability specs defined
  - add-user-auth change archived

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-28 23:41:37 +08:00

4.1 KiB
Raw Blame History

Tasks: add-user-auth

1. 專案初始化

  • 1.1 建立 Conda 環境與 requirements.txt
  • 1.2 初始化 FastAPI 專案結構
  • 1.3 設定 MySQL 連線與 SQLAlchemy
  • 1.4 設定 Redis 連線
  • 1.5 建立環境變數配置 (.env.example)

2. 資料庫模型

  • 2.1 建立 pjctrl_roles 資料表 migration
  • 2.2 建立 pjctrl_departments 資料表 migration
  • 2.3 建立 pjctrl_users 資料表 migration
  • 2.4 建立 seed data (預設管理員與角色)
  • 2.5 驗證 migration 可正確執行與回滾

3. 認證模組

  • 3.1 實作外部 API 認證 client (app/services/auth_client.py)
  • 3.2 實作 JWT Token 驗證邏輯
  • 3.3 實作登入 API endpoint (POST /api/auth/login)
  • 3.4 實作登出 API endpoint (POST /api/auth/logout)
  • 3.5 實作取得當前使用者 API (GET /api/auth/me)
  • 3.6 處理認證 API 連線失敗情境

4. Session 管理

  • 4.1 實作 Redis Session 儲存
  • 4.2 實作 Session 過期機制
  • 4.3 實作 Token 刷新機制 (如需要) - 暫不需要

5. 權限控制

  • 5.1 實作認證中間件 (app/middleware/auth.py)
  • 5.2 實作 RBAC 權限檢查裝飾器
  • 5.3 實作部門隔離邏輯
  • 5.4 實作系統管理員全域權限判斷

6. 使用者管理 API

  • 6.1 實作使用者列表 API (GET /api/users)
  • 6.2 實作使用者詳情 API (GET /api/users/{id})
  • 6.3 實作角色指派 API (PATCH /api/users/{id}/role)
  • 6.4 實作部門管理 API (CRUD)

7. 測試

  • 7.1 撰寫認證模組單元測試
  • 7.2 撰寫權限檢查單元測試
  • 7.3 撰寫 API 整合測試
  • 7.4 測試系統管理員權限
  • 7.5 測試部門隔離情境

8. 前端 (基礎)

  • 8.1 建立 React 專案結構
  • 8.2 實作 AuthContext (認證狀態管理)
  • 8.3 實作登入頁面
  • 8.4 實作 Protected Route 元件
  • 8.5 實作登出功能

Dependencies

1.x (專案初始化) → 2.x (資料庫) → 3.x (認證) → 4.x (Session)
                                    ↓
                              5.x (權限) → 6.x (使用者管理)
                                    ↓
                              7.x (測試)

8.x (前端) 可與 3.x-6.x 並行開發

Notes

  • 所有資料表使用 pjctrl_ 前綴
  • 認證必須透過外部 API不可有本地繞過
  • 系統管理員帳號在 seed data 中建立

Implementation Summary

完成日期: 2024-01-XX

已建立的檔案結構

backend/
├── app/
│   ├── api/
│   │   ├── auth/router.py      # 認證 API
│   │   ├── users/router.py     # 使用者管理 API
│   │   └── departments/router.py # 部門管理 API
│   ├── core/
│   │   ├── config.py           # 環境配置
│   │   ├── database.py         # 資料庫連線
│   │   ├── redis.py            # Redis 連線
│   │   └── security.py         # JWT 處理
│   ├── middleware/
│   │   └── auth.py             # 認證與權限中間件
│   ├── models/
│   │   ├── user.py             # User model
│   │   ├── role.py             # Role model
│   │   └── department.py       # Department model
│   ├── schemas/                # Pydantic schemas
│   ├── services/
│   │   └── auth_client.py      # 外部認證 API client
│   └── main.py                 # FastAPI 應用程式
├── migrations/
│   └── versions/
│       └── 001_initial_auth_tables.py
├── tests/
│   ├── conftest.py
│   ├── test_auth.py
│   └── test_users.py
├── .env
├── .env.example
├── requirements.txt
└── environment.yml

frontend/
├── src/
│   ├── components/
│   │   └── ProtectedRoute.tsx
│   ├── contexts/
│   │   └── AuthContext.tsx
│   ├── pages/
│   │   ├── Login.tsx
│   │   └── Dashboard.tsx
│   ├── services/
│   │   └── api.ts
│   ├── App.tsx
│   └── main.tsx
├── package.json
├── tsconfig.json
└── vite.config.ts