Skip to content

Commit e8335c1

Browse files
Copilothotlong
andcommitted
refactor: address code review - use randomUUID, named constant, and improve JSDoc
Co-authored-by: hotlong <50353452+hotlong@users.noreply.github.com>
1 parent 2eddaa8 commit e8335c1

2 files changed

Lines changed: 15 additions & 6 deletions

File tree

packages/plugins/plugin-auth/src/auth-plugin.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ export interface AuthPluginOptions extends Partial<AuthConfig> {
2727
*
2828
* Provides authentication and identity services for ObjectStack applications.
2929
*
30+
* **Dual-Mode Operation:**
31+
* - **Server mode** (HonoServerPlugin active): Registers HTTP routes at basePath,
32+
* forwarding all auth requests to better-auth's universal handler.
33+
* - **MSW/Mock mode** (no HTTP server): Gracefully skips route registration but
34+
* still registers the `auth` service, allowing HttpDispatcher.handleAuth() to
35+
* simulate auth flows (sign-up, sign-in, etc.) for development and testing.
36+
*
3037
* Features:
3138
* - Session management
3239
* - User registration/login
@@ -35,8 +42,8 @@ export interface AuthPluginOptions extends Partial<AuthConfig> {
3542
* - 2FA, passkeys, magic links
3643
*
3744
* This plugin registers:
38-
* - `auth` service (auth manager instance)
39-
* - HTTP routes for authentication endpoints
45+
* - `auth` service (auth manager instance) — always
46+
* - HTTP routes for authentication endpoints — only when HTTP server is available
4047
*
4148
* Integrates with better-auth library to provide comprehensive
4249
* authentication capabilities including email/password, OAuth, 2FA,

packages/runtime/src/http-dispatcher.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import { ObjectKernel, getEnv } from '@objectstack/core';
44
import { CoreServiceName } from '@objectstack/spec/system';
5+
import { randomUUID } from 'crypto';
56

67
export interface HttpProtocolContext {
78
request: any;
@@ -202,32 +203,33 @@ export class HttpDispatcher {
202203
*/
203204
private mockAuthFallback(path: string, method: string, body: any): HttpDispatcherResult {
204205
const m = method.toUpperCase();
206+
const MOCK_SESSION_EXPIRY_MS = 86_400_000; // 24 hours
205207

206208
// POST sign-up/email
207209
if ((path === 'sign-up/email' || path === 'register') && m === 'POST') {
208-
const id = `mock_${Date.now()}`;
210+
const id = `mock_${randomUUID()}`;
209211
return {
210212
handled: true,
211213
response: {
212214
status: 200,
213215
body: {
214216
user: { id, name: body?.name || 'Mock User', email: body?.email || 'mock@test.local', emailVerified: false, createdAt: new Date().toISOString(), updatedAt: new Date().toISOString() },
215-
session: { id: `session_${id}`, userId: id, token: `mock_token_${id}`, expiresAt: new Date(Date.now() + 86400000).toISOString() },
217+
session: { id: `session_${id}`, userId: id, token: `mock_token_${id}`, expiresAt: new Date(Date.now() + MOCK_SESSION_EXPIRY_MS).toISOString() },
216218
},
217219
},
218220
};
219221
}
220222

221223
// POST sign-in/email or login
222224
if ((path === 'sign-in/email' || path === 'login') && m === 'POST') {
223-
const id = `mock_${Date.now()}`;
225+
const id = `mock_${randomUUID()}`;
224226
return {
225227
handled: true,
226228
response: {
227229
status: 200,
228230
body: {
229231
user: { id, name: 'Mock User', email: body?.email || 'mock@test.local', emailVerified: true, createdAt: new Date().toISOString(), updatedAt: new Date().toISOString() },
230-
session: { id: `session_${id}`, userId: id, token: `mock_token_${id}`, expiresAt: new Date(Date.now() + 86400000).toISOString() },
232+
session: { id: `session_${id}`, userId: id, token: `mock_token_${id}`, expiresAt: new Date(Date.now() + MOCK_SESSION_EXPIRY_MS).toISOString() },
231233
},
232234
},
233235
};

0 commit comments

Comments
 (0)