-
Notifications
You must be signed in to change notification settings - Fork 0
Fix user role assignment in admin user creation flow #148
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
…to use it Co-authored-by: totallynotdavid <20960328+totallynotdavid@users.noreply.github.com>
Deploying limalimpia with
|
| Latest commit: |
08fbcf4
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://79b01359.limalimpia.pages.dev |
| Branch Preview URL: | https://copilot-fix-add-new-user-iss.limalimpia.pages.dev |
|
@copilot fix the tests |
Co-authored-by: totallynotdavid <20960328+totallynotdavid@users.noreply.github.com>
Fixed in commit 3d5676d. The issue was that better-auth's organization plugin manages roles via organization membership, not as a direct user property. The |
|
@copilot Make a final review of your changes! Be critical |
Co-authored-by: totallynotdavid <20960328+totallynotdavid@users.noreply.github.com>
Completed critical review. Found and fixed 2 critical issues in commit d74e0f5: Critical Issue #1: Orphaned Users
Critical Issue #2: Incorrect Error Handling
Other observations:
The fix now properly handles all failure scenarios and prevents data inconsistency. |
Fix Add New User Role Assignment Issue ✅
The issue is that when creating a new user through the "Add User" dialog, the role is not being properly passed to the backend. Users are being created with the default role of "citizen" instead of their selected role (admin, supervisor, or driver).
Root Cause
The
signUpaction inapps/web/src/features/auth/actions.tscalls/api/auth/sign-up/email, which is the standard better-auth signup endpoint. This endpoint doesn't support role assignment and defaults to "citizen" (as configured inapps/api/src/internal/domains/auth/service.ts).Additionally, the better-auth organization plugin manages roles via organization membership in the
membertable, not as a direct user property. Simply passing aroleparameter tocreateUserdoesn't create the organization membership.Changes Made
apps/api/src/internal/domains/admin/schemas.ts): AddedCreateUserSchemathat includes role validationapps/api/src/internal/domains/admin/service.ts):createUsermethod with proper transaction-like behavior:handleDatabaseErrorfor DB errors andhandleAuthApiErrorfor auth errorsapps/api/src/internal/domains/admin/handler.ts):/api/admin/usersendpoint that validates and creates users with rolesapps/api/src/internal/container/container.ts): Pass database to AdminService constructorapps/web/src/lib/api.ts): AddedcreateUsermethod to admin API clientapps/web/src/features/auth/actions.ts): UpdatedsignUpto use the new admin endpoint instead of the generic signupapps/api/tests/admin.test.ts): Added comprehensive test cases for creating users with different roles (admin, supervisor, driver) and validating role rejectionCritical Improvements in Latest Commit
handleDatabaseError, auth errors usehandleAuthApiErrorSolution Plan
Security Summary
The fix ensures that when an admin/supervisor creates a new user through the UI, the user is properly added to the organization with the selected role (admin, supervisor, or driver) via the
membertable. If any step fails, appropriate cleanup is performed to maintain data integrity.Original prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.