Skip to content

[Bug] Auth 服务 /api/v1/auth/* 路由无法访问(插件注册顺序导致未注册) #883

Description

@hotlong

问题描述

在 monorepo 根目录运行 pnpm dev 启动开发环境后,无法访问 http://localhost:3000/api/v1/auth/get-session 等 Auth 服务接口。

  • 实际结果:返回 404,未注册 auth 路由。
  • 期望结果:可访问 api/v1/auth/* 系列接口,正常获取 session。

复现步骤

  1. pnpm dev
  2. 打开浏览器访问 http://localhost:3000/api/v1/auth/get-session
  3. 出现 404 或无法连接

根因分析

  • objectstack.config.tsplugins 数组中,AuthPluginHonoServerPlugin 之前注册。
  • CLI 的 serve 命令会先注册所有 config.plugins,然后最后才注册 HonoServerPlugin,导致 AuthPlugin 的路由注册时获取不到 HTTP 服务实例。
  • AuthPlugin.start() 检测不到 http-server,打印 warn:No HTTP server available — auth routes not registered.

相关代码片段

import { defineStack } from '@objectstack/spec';
import { AppPlugin, DriverPlugin } from '@objectstack/runtime';
import { ObjectQLPlugin } from '@objectstack/objectql';
import { InMemoryDriver } from '@objectstack/driver-memory';
import { HonoServerPlugin } from '@objectstack/plugin-hono-server';  // ← 新增
import CrmApp from './examples/app-crm/objectstack.config';
import TodoApp from './examples/app-todo/objectstack.config';
import BiPlugin from './examples/plugin-bi/objectstack.config';
import { AuthPlugin } from '@objectstack/plugin-auth';

export default defineStack({
  manifest: {
    id: 'dev-workspace',
    name: 'dev_workspace',
    version: '0.0.0',
    description: 'ObjectStack monorepo development workspace',
    type: 'app',
  },
  plugins: [
    new ObjectQLPlugin(),
    new DriverPlugin(new InMemoryDriver()),
    new HonoServerPlugin({ port: 3000 }),  // ← 必须在 AuthPlugin 之前
    new AuthPlugin({
      secret: 'dev-secret-please-change-in-production-min-32-chars',
      baseUrl: 'http://localhost:3000',
    }),
    new AppPlugin(CrmApp),
    new AppPlugin(TodoApp),
    new AppPlugin(BiPlugin),
  ],
});

建议修复方案

  1. 优先注册 HonoServerPlugin —— objectstack.config.tsplugins 数组必须先注册 HTTP 服务,再注册 AuthPlugin。
  2. CLI 应在遍历 config.plugins 之前注册 HonoServerPlugin。(或检测是否重复注册,避免多次实例化)
  3. AuthPlugin 可用 kernel:ready hook 延迟注册路由,兼容不同插件加载顺序。

Metadata

Metadata

Labels

bugSomething isn't working

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions