feat: build admin API + security fixes #66 - #66
Open
TarekHassan1 wants to merge 2 commits into
Open
Conversation
Author
|
For last commit Closes #62 Security & access-control fixes1.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #61
Summary
Implements the full admin API so deployers no longer need to hand-write SQL to create vote items or manage admin status. Admin identity is stored in D1 (not Clerk metadata), and the first admin can be bootstrapped via an environment variable with zero manual SQL.
Admin identity
apps/api/src/middleware/adminAuth.ts—requireAdminresolvesgetAuth(c), looks the user up in D1, and returns403unlessis_admin = 1.First-admin bootstrap
apps/api/src/utils/user.ts—ensureUserExistschecks the optionalADMIN_EMAILScomma-separated Worker variable.requireAuthmiddleware or the Clerk webhook),is_adminis set to1automatically.Schema changes
is_adminboolean column to the existinguserstable.settingstable (key/value) for app-level configuration.New routes (
/api/v1/admin, all behindrequireAdmin)GET/itemsPOST/itemsnameand uniqueqrSlug; returns409on duplicate slugPUT/items/:idDELETE/items/:idPOST/items/bulkGET/settingsPUT/settingsGET/usersPUT/users/:id/adminFrontend route guard
GET /api/v1/user/me— returns{ isAdmin }so the frontend can conditionally render admin navigation.Webhook update
apps/api/src/controllers/webhook.Controller.ts— now usesensureUserExistssouser.created/user.updatedevents respectADMIN_EMAILS.Manual fallback
If
ADMIN_EMAILSwasn't set at deploy time, a user can be promoted manually: