Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 8
feat: universal AuthPlugin — graceful HTTP server handling, mock auth fallback, and browser-safe runtime#863
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
8a4beec2eddaa8e8335c1aa1c4e2aba032312fdc1fcefcee218b3e84711e696File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -27,6 +27,13 @@ export interface AuthPluginOptions extends Partial<AuthConfig> { | ||
| * | ||
| * Provides authentication and identity services for ObjectStack applications. | ||
| * | ||
| * **Dual-Mode Operation:** | ||
| * - **Server mode** (HonoServerPlugin active): Registers HTTP routes at basePath, | ||
| * forwarding all auth requests to better-auth's universal handler. | ||
| * - **MSW/Mock mode** (no HTTP server): Gracefully skips route registration but | ||
| * still registers the `auth` service, allowing HttpDispatcher.handleAuth() to | ||
| * simulate auth flows (sign-up, sign-in, etc.) for development and testing. | ||
| * | ||
| * Features: | ||
| * - Session management | ||
| * - User registration/login | ||
| @@ -35,8 +42,8 @@ export interface AuthPluginOptions extends Partial<AuthConfig> { | ||
| * - 2FA, passkeys, magic links | ||
| * | ||
| * This plugin registers: | ||
| * - `auth` service (auth manager instance) | ||
| * - HTTP routes for authentication endpoints | ||
| * - `auth` service (auth manager instance) — always | ||
| * - HTTP routes for authentication endpoints — only when HTTP server is available | ||
| * | ||
| * Integrates with better-auth library to provide comprehensive | ||
| * authentication capabilities including email/password, OAuth, 2FA, | ||
| @@ -46,7 +53,7 @@ export class AuthPlugin implements Plugin { | ||
| name = 'com.objectstack.auth'; | ||
| type = 'standard'; | ||
| version = '1.0.0'; | ||
| dependencies= ['com.objectstack.server.hono']; // Requires HTTP server | ||
| dependencies: string[] = []; // HTTP server is optional; routes are registered only when available | ||
| private options: AuthPluginOptions; | ||
| private authManager: AuthManager | null = null; | ||
| @@ -92,16 +99,24 @@ export class AuthPlugin implements Plugin { | ||
| throw new Error('Auth manager not initialized'); | ||
| } | ||
| // Register HTTP routes if enabled | ||
| // Register HTTP routes if enabled and HTTP server is available | ||
| if (this.options.registerRoutes) { | ||
| let httpServer: IHttpServer | null = null; | ||
| try { | ||
| const httpServer = ctx.getService<IHttpServer>('http-server'); | ||
| httpServer = ctx.getService<IHttpServer>('http-server'); | ||
| } catch { | ||
| // Service not found — expected in MSW/mock mode | ||
| } | ||
| if (httpServer) { | ||
| // Route registration errors should propagate (server misconfiguration) | ||
| this.registerAuthRoutes(httpServer, ctx); | ||
| ctx.logger.info(`Auth routes registered at ${this.options.basePath}`); | ||
| } catch (error) { | ||
| const err = error instanceof Error ? error : new Error(String(error)); | ||
| ctx.logger.error('Failed to register auth routes:', err); | ||
| throw err; | ||
| } else { | ||
| ctx.logger.warn( | ||
| 'No HTTP server available — auth routes not registered. ' + | ||
| 'Auth service is still available for MSW/mock environments via HttpDispatcher.' | ||
| ); | ||
| } | ||
Comment on lines
103
to
120
CopilotAI | ||
| } | ||
CopilotAIMar 3, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Docs say mock fallback happens "when no auth service handler or broker is available", but the implementation falls back for most auth paths whenever the auth service handler is missing (even if a broker exists). Please align this wording with the actual fallback conditions (e.g., auth service missing and legacy broker login unavailable/unsupported).