Skip to content

feat(auth): add membership and role checks to server mutation routes#88

Closed
varun29sharma wants to merge 1 commit into
0rigin-c0de:mainfrom
varun29sharma:feat/server-role-auth-issue-19
Closed

feat(auth): add membership and role checks to server mutation routes#88
varun29sharma wants to merge 1 commit into
0rigin-c0de:mainfrom
varun29sharma:feat/server-role-auth-issue-19

Conversation

@varun29sharma
Copy link
Copy Markdown
Contributor

Summary

Closes #19

Adds authentication, server membership verification, and role-based access
control to the three unprotected mutation routes in server/routes/servers.js.
Previously any anonymous HTTP request could create channels, categories, or
delete a server entirely.

Changes

New file: server/middleware/verifyServerRole.js

A three-layer guard middleware that runs in sequence:

  1. Verifies JWT from x-auth-token header → 401 if missing or invalid
  2. Calls existing checkServerInUser() to confirm user belongs to the server → 403 if not a member
  3. Checks server_role on the User document is owner or admin403 if regular member

Attaches req.user and req.serverMembership for downstream use.

Modified: server/routes/servers.js

Route Before After
POST /add_new_channel No auth at all validate()verifyServerRole() → handler
POST /add_new_category No auth at all validate()verifyServerRole() → handler
POST /delete_server No auth at all validate()verifyServerRole() → handler

Reuses the existing checkServerInUser() from serverService.js — the same
pattern already used correctly in /server_info.
Socket.IO server_updated events are completely unchanged.

Test evidence

All tests run with curl against local instance (port 2000):

  • POST /add_new_channel with no token → 401
  • POST /add_new_channel with fake token → 401
  • POST /add_new_channel with valid token but wrong server → 403
  • POST /add_new_category with owner token + real server → 200

Compatibility

All existing happy-path response shapes unchanged. No frontend code modified.
validate() middleware from PR #82 runs first, verifyServerRole() second —
the two middlewares compose cleanly.Yeah So everything's fine now

- Add server/middleware/verifyServerRole.js with 3-layer guard:
  401 for missing/invalid JWT
  403 for non-members of the target server
  403 for members without owner/admin role
- Apply verifyServerRole to /add_new_channel, /add_new_category, /delete_server
- Reuses existing checkServerInUser() from serverService.js
- validate() from Issue 18 runs first, verifyServerRole second
- Socket.IO server_updated events completely unchanged

Closes 0rigin-c0de#19
@netlify
Copy link
Copy Markdown

netlify Bot commented May 17, 2026

👷 Deploy request for piperchat01 pending review.

Visit the deploys page to approve it

Name Link
🔨 Latest commit 57d67fb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Prevent unauthorized channel and category changes by non-members

1 participant