Issue 134
tech-debt: Accounts module: one getCurrentUser() over the Local API; sign-in as server actions
Back to our planDone
cmsauthtech-debtpriority: medium
Type
tech-debt
Summary
Create one place that answers "who is signed in and what may they do", so future features (resident accounts, comments, permissions) build on a single, tested foundation instead of each page doing it its own way.
Problem
- 4 files call Payload's user endpoints by hand (5 calls):
hooks/useAuth.ts(/me,/logout),login/LoginForm.tsx,account/ChangePasswordForm.tsx(PATCH /api/users/:id),utilities/getMeUser.ts. getMeUserruns on the server but checks the session by calling the site's own HTTP API, instead of Payload's Local API (payload.auth({ headers }), https://payloadcms.com/docs/authentication/operations, opens in a new tab).hooks/useAuth.tsis a React client hook filed next to Payload's server hooks.- Next.js recommends authorization checks inside a server-only data layer, not only in pages or middleware (https://nextjs.org/docs/app/guides/data-security, opens in a new tab).
Proposed fix
Create src/modules/accounts/:
data/:getCurrentUser()(cached per request, Local API).use-cases/:requireUser()/requireRole(role), exported fromserver.ts.actions.ts: sign in, sign out and change password as server actions that validate input and re-check the user.- Role checks and access functions stay shared in
src/platform/payload/(moved fromsrc/access/), because every module uses them andplatform/imports no module (docs/architecture/README.md, rule 3).accountsowns the role field. cms/: the Users collection.ui/: login form, change-password form, the header's account menu state.
Acceptance criteria
- Not done: No component or page calls
/api/users/*directly. - Not done: Role checks exist once and are unit-tested; Payload access functions use them.
- Not done: Login, logout,
/account, change password and the header's signed-in state work as before (checked locally). - Not done: Error messages on sign-in stay generic (no account enumeration).
Depends on
#129 and #130. Before resident sign-up opens, also #140 (via #143) and #147. Step 9 of #127.
Area
auth, cms
Priority
medium
Generated by Claude Code, opens in a new tab