feat: add admin area with item CRUD, Lexical editor and access control (M6)

Backend:
- Item model (title, url, points, username, content, created_at)
- is_admin flag on User + startup migration for pre-existing DBs
- get_current_admin dependency (401 without/invalid token, 403 for
  non-admins); server-side check on every admin endpoint
- /api/v1/admin/items CRUD: pagination, case-insensitive title/url
  search, sort by title/created_at/points, in-memory rate limiting
  (1000 req/60s per admin -> 429)
- seed_admin.py (ADMIN_USERNAME/ADMIN_PASSWORD), documented in
  backend/.env.example

Frontend:
- Auth context exposes is_admin; RequireAdmin route guard (redirects
  signed-out users, renders 403 page for non-admins)
- AdminLayout with responsive dark sidebar; Admin link in header for
  admins only
- AdminDashboard: paginated table (page size 10/20/50), 300ms
  debounced search, sortable title/points/created_at columns,
  edit/delete row actions
- EditItemPage: create + edit with client-side validation and a
  Lexical (0.25) WYSIWYG editor (bold/italic/underline, H1-H6,
  lists, code blocks, links, undo/redo), optional debounced auto-save
- DeleteConfirmation modal with warning icon and explicit
  "Yes, delete" confirmation
- Unit tests (34) and mocked E2E workflow tests (10)

All acceptance criteria for milestones/m6-setup-admin-area-prompt.md
verified: 108 backend, 87 frontend unit and 29 E2E tests pass.
This commit is contained in:
Daniel Samson committed 2026-10-30 21:36:15 +00:00
1 parent b186d0f797
commit b440809548
38 files changed
+5415 -50

No files matched your search

+13 -5
View File
@@ -42,7 +42,11 @@ def register(payload: RegisterIn, db: Session = Depends(get_db)) -> TokenOut:
# Lost the race against a concurrent registration.
db.rollback()
raise HTTPException(status_code=400, detail="Username already exists") from None
return TokenOut(token=auth.create_token(user.username), username=user.username)
return TokenOut(
token=auth.create_token(user.username),
username=user.username,
is_admin=user.is_admin,
)
@router.post("/login", response_model=TokenOut, summary="Log in with credentials")
@@ -51,7 +55,11 @@ def login(payload: LoginIn, db: Session = Depends(get_db)) -> TokenOut:
user = _user_by_username(db, payload.username)
if user is None or not auth.verify_password(payload.password, user.password_hash):
raise HTTPException(status_code=401, detail="Invalid username or password")
return TokenOut(token=auth.create_token(user.username), username=user.username)
return TokenOut(
token=auth.create_token(user.username),
username=user.username,
is_admin=user.is_admin,
)
@router.post(
@@ -65,6 +73,6 @@ def logout(current_user: str = Depends(auth.get_current_user)) -> None:
@router.get("/me", response_model=UserOut, summary="Current authenticated user")
def me(current_user: str = Depends(auth.get_current_user)) -> UserOut:
"""Return the user the Bearer token belongs to."""
return UserOut(username=current_user)
def me(current_user: User = Depends(auth.get_current_user_obj)) -> UserOut:
"""Return the user the Bearer token belongs to (including ``is_admin``)."""
return UserOut(username=current_user.username, is_admin=current_user.is_admin)