Skip to content

Commit aa1c4e2

Browse files
Copilothotlong
andcommitted
feat: add AuthPlugin to studio kernel factory and document MSW/mock mode
- Add AuthPlugin to apps/studio createKernel.ts for default auth in MSW mode - Add @objectstack/plugin-auth dependency to studio package.json - Update authentication.mdx: HTTP server is optional, add MSW/Mock Mode section with minimal config example, mock fallback endpoints table, and studio usage Co-authored-by: hotlong <50353452+hotlong@users.noreply.github.com>
1 parent e8335c1 commit aa1c4e2

4 files changed

Lines changed: 77 additions & 1 deletion

File tree

apps/studio/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"@objectstack/driver-memory": "workspace:*",
2323
"@objectstack/metadata": "workspace:*",
2424
"@objectstack/objectql": "workspace:*",
25+
"@objectstack/plugin-auth": "workspace:*",
2526
"@objectstack/plugin-msw": "workspace:*",
2627
"@objectstack/runtime": "workspace:*",
2728
"@objectstack/spec": "workspace:*",

apps/studio/src/mocks/createKernel.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { ObjectKernel, DriverPlugin, AppPlugin } from '@objectstack/runtime';
44
import { ObjectQLPlugin, SchemaRegistry } from '@objectstack/objectql';
55
import { InMemoryDriver } from '@objectstack/driver-memory';
66
import { MSWPlugin } from '@objectstack/plugin-msw';
7+
import { AuthPlugin } from '@objectstack/plugin-auth';
78

89
export interface KernelOptions {
910
appConfigs?: any[]; // Multiple app configs
@@ -42,6 +43,12 @@ export async function createKernel(options: KernelOptions) {
4243
// ObjectQLPlugin's ctx.registerService('protocol', ...) during bootstrap.
4344
console.log('[KernelFactory] Protocol service will be registered by ObjectQLPlugin');
4445

46+
// Register AuthPlugin for MSW/mock mode (gracefully skips HTTP route registration)
47+
await kernel.use(new AuthPlugin({
48+
secret: 'mock-dev-secret-at-least-32-characters-long',
49+
baseUrl: 'http://localhost:5173',
50+
}));
51+
4552
// --- BROKER SHIM (MUST be registered BEFORE MSWPlugin) ---
4653
// HttpDispatcher requires a broker to function. We inject a shim.
4754
(kernel as any).broker = {

content/docs/guides/authentication.mdx

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Complete guide to implementing authentication in ObjectStack applications using
1818
7. [Client Integration](#client-integration)
1919
8. [API Reference](#api-reference)
2020
9. [Best Practices](#best-practices)
21+
10. [MSW/Mock Mode](#mswmock-mode)
2122

2223
---
2324

@@ -89,7 +90,7 @@ import { HonoServerPlugin } from '@objectstack/plugin-hono-server';
8990

9091
const kernel = new ObjectKernel({
9192
plugins: [
92-
// HTTP server (required for auth routes)
93+
// HTTP server (optional — auth works without it in MSW/mock mode)
9394
new HonoServerPlugin({
9495
port: 3000,
9596
}),
@@ -107,6 +108,10 @@ await kernel.start();
107108

108109
That's it! Your authentication endpoints are now available at `/api/v1/auth/*`.
109110

111+
> **MSW/Mock Mode:** AuthPlugin does **not** require an HTTP server. When HonoServerPlugin
112+
> is absent, the plugin gracefully skips route registration and still registers the `auth`
113+
> service. See [MSW/Mock Mode](#mswmock-mode) below for details.
114+
110115
### 3. ObjectQL Data Persistence
111116

112117
The plugin automatically uses ObjectQL for data persistence. No additional database configuration is required - it works with your existing ObjectQL setup.
@@ -586,6 +591,66 @@ better-auth internally uses model names like `user` and `session`. The ObjectQL
586591
587592
---
588593

594+
## MSW/Mock Mode
595+
596+
AuthPlugin is designed to work in **both** server and MSW/mock (browser-only) environments. This means you can develop and test authentication flows without running a real HTTP server.
597+
598+
### How It Works
599+
600+
- **Server mode** (HonoServerPlugin active): AuthPlugin registers HTTP routes at `/api/v1/auth/*` and forwards all requests to better-auth.
601+
- **MSW/mock mode** (no HTTP server): AuthPlugin gracefully skips route registration but still registers the `auth` service. The `HttpDispatcher` provides mock fallback responses for core auth endpoints.
602+
603+
### Minimal Configuration for Mock Mode
604+
605+
```typescript
606+
import { ObjectKernel, DriverPlugin, AppPlugin } from '@objectstack/runtime';
607+
import { ObjectQLPlugin } from '@objectstack/objectql';
608+
import { InMemoryDriver } from '@objectstack/driver-memory';
609+
import { AuthPlugin } from '@objectstack/plugin-auth';
610+
611+
const kernel = new ObjectKernel();
612+
613+
await kernel.use(new ObjectQLPlugin());
614+
await kernel.use(new DriverPlugin(new InMemoryDriver(), 'memory'));
615+
616+
// AuthPlugin works without HonoServerPlugin — no HTTP server needed
617+
await kernel.use(new AuthPlugin({
618+
secret: 'mock-dev-secret-at-least-32-characters-long',
619+
baseUrl: 'http://localhost:5173',
620+
}));
621+
622+
await kernel.bootstrap();
623+
```
624+
625+
### Mock Fallback Endpoints
626+
627+
When no auth service handler or broker is available, `HttpDispatcher.handleAuth()` automatically provides mock responses for:
628+
629+
| Endpoint | Method | Description |
630+
|:---|:---:|:---|
631+
| `sign-up/email` | POST | Returns mock user + session |
632+
| `sign-in/email` | POST | Returns mock user + session |
633+
| `login` | POST | Legacy login — returns mock user + session |
634+
| `register` | POST | Alias for sign-up |
635+
| `get-session` | GET | Returns `{ session: null, user: null }` |
636+
| `sign-out` | POST | Returns `{ success: true }` |
637+
638+
This ensures that registration and sign-in flows do not return 404 errors in MSW/browser-only environments.
639+
640+
### Studio Kernel Factory
641+
642+
The Studio app's `createKernel()` factory includes AuthPlugin by default:
643+
644+
```typescript
645+
// apps/studio/src/mocks/createKernel.ts
646+
await kernel.use(new AuthPlugin({
647+
secret: 'mock-dev-secret-at-least-32-characters-long',
648+
baseUrl: 'http://localhost:5173',
649+
}));
650+
```
651+
652+
---
653+
589654
## Next Steps
590655

591656
- See [Security Guide](/docs/guides/security) for authorization and permissions

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)