Add authentication and workspace middleware #21
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.
Summary
Implemented middleware infrastructure for authentication, workspace context injection, and global error handling. This provides a foundation for protecting routes and ensuring proper user/workspace context is available throughout the API.
Implementation
Created Middleware Files
apps/server/src/middleware/auth.tsauthenticateRequest(): Validates user session using better-auth'sgetSession()APIrequireAuth(): Middleware to enforce authentication on routes (returns 401 if unauthenticated)AuthenticatedUserinterfaceapps/server/src/middleware/workspace.tsgetUserWorkspace(): Fetches user's workspace from databaserequireWorkspace(): Middleware to inject workspace context (returns 404 if no workspace found)WorkspaceContextinterfaceapps/server/src/middleware/error-handler.tshandleError(): Global error handler for Elysia applicationAppErrorclass for application-specific errorsApplied to Server
apps/server/src/index.ts.onError()hook/api/protected/*route group with authentication and workspace middleware.derive()to injectuserandworkspaceinto request context/api/protected/meendpoint demonstrating middleware usageTesting
Review Checklist
Closes https://linear.app/megaforce/issue/MEG-13/add-authentication-and-workspace-middleware
🤖 Generated with Claude Code