auth-core-users.ts87 lines · main
| 1 | /** |
| 2 | * briven-engine users API (Phase 5 surface). |
| 3 | * |
| 4 | * GET /v1/auth-core/users |
| 5 | * GET /v1/auth-core/users/:userId/metadata |
| 6 | * PUT /v1/auth-core/users/:userId/metadata |
| 7 | */ |
| 8 | |
| 9 | import { Hono } from 'hono'; |
| 10 | |
| 11 | import { requireAuthCoreDashboard } from '../middleware/auth-core-guard.js'; |
| 12 | import { BRIVEN_ENGINE_ID, isAuthCoreInitialized } from '../services/auth-core/engine.js'; |
| 13 | import { |
| 14 | getBrivenEngineUserMetadata, |
| 15 | listBrivenEngineUsers, |
| 16 | updateBrivenEngineUserMetadata, |
| 17 | } from '../services/auth-core/users.js'; |
| 18 | import type { AppEnv } from '../types/app-env.js'; |
| 19 | |
| 20 | export const authCoreUsersRouter = new Hono<AppEnv>(); |
| 21 | |
| 22 | authCoreUsersRouter.use('/v1/auth-core/users', requireAuthCoreDashboard()); |
| 23 | authCoreUsersRouter.use('/v1/auth-core/users/*', requireAuthCoreDashboard()); |
| 24 | |
| 25 | authCoreUsersRouter.get('/v1/auth-core/users', async (c) => { |
| 26 | if (!isAuthCoreInitialized()) { |
| 27 | return c.json( |
| 28 | { engine: BRIVEN_ENGINE_ID, code: 'auth_core_sdk_not_ready', users: [] }, |
| 29 | 503, |
| 30 | ); |
| 31 | } |
| 32 | const limit = Number(c.req.query('limit') ?? '50'); |
| 33 | const paginationToken = c.req.query('paginationToken') ?? undefined; |
| 34 | const projectId = c.req.query('projectId') ?? undefined; |
| 35 | let tenantId = c.req.query('tenantId') ?? undefined; |
| 36 | if (!tenantId && projectId) { |
| 37 | try { |
| 38 | const { projectIdToTenantId } = await import( |
| 39 | '../services/auth-core/project-map.js' |
| 40 | ); |
| 41 | tenantId = projectIdToTenantId(projectId); |
| 42 | } catch { |
| 43 | tenantId = undefined; |
| 44 | } |
| 45 | } |
| 46 | const result = await listBrivenEngineUsers({ |
| 47 | limit: Number.isFinite(limit) ? limit : 50, |
| 48 | paginationToken, |
| 49 | tenantId, |
| 50 | }); |
| 51 | return c.json({ |
| 52 | ...result, |
| 53 | projectId: projectId ?? null, |
| 54 | tenantId: tenantId ?? null, |
| 55 | }); |
| 56 | }); |
| 57 | |
| 58 | authCoreUsersRouter.get('/v1/auth-core/users/:userId/metadata', async (c) => { |
| 59 | const userId = c.req.param('userId'); |
| 60 | const metadata = await getBrivenEngineUserMetadata(userId); |
| 61 | if (metadata == null && !isAuthCoreInitialized()) { |
| 62 | return c.json({ engine: BRIVEN_ENGINE_ID, code: 'auth_core_sdk_not_ready' }, 503); |
| 63 | } |
| 64 | return c.json({ engine: BRIVEN_ENGINE_ID, userId, metadata: metadata ?? {} }); |
| 65 | }); |
| 66 | |
| 67 | authCoreUsersRouter.put('/v1/auth-core/users/:userId/metadata', async (c) => { |
| 68 | const userId = c.req.param('userId'); |
| 69 | let body: Record<string, unknown> = {}; |
| 70 | try { |
| 71 | body = (await c.req.json()) as Record<string, unknown>; |
| 72 | } catch { |
| 73 | body = {}; |
| 74 | } |
| 75 | const ok = await updateBrivenEngineUserMetadata(userId, body); |
| 76 | if (!ok) { |
| 77 | return c.json( |
| 78 | { |
| 79 | engine: BRIVEN_ENGINE_ID, |
| 80 | ok: false, |
| 81 | code: isAuthCoreInitialized() ? 'update_failed' : 'auth_core_sdk_not_ready', |
| 82 | }, |
| 83 | isAuthCoreInitialized() ? 400 : 503, |
| 84 | ); |
| 85 | } |
| 86 | return c.json({ engine: BRIVEN_ENGINE_ID, ok: true, userId }); |
| 87 | }); |