[GH-ISSUE #670] How to connect auth with OpenAPIHono ? #8369

Closed
opened 2026-04-13 03:26:26 -05:00 by GiteaMirror · 8 comments
Owner

Originally created by @Malin88 on GitHub (Nov 26, 2024).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/670

I'm trying to do it this way:

const create = createRoute({
  tags: ["auth"],
  method: "post",
  path: "/api/auth/**",
  request: {
    body: {
      content: {
        "application/json": {
          schema: {
            name: z.string(),
            email: z.string().email(),
            password: z.string(),
          },
        },
      },
      description: "Register user",
      required: true,
    },
  },
  responses: {
    200: {
      description: "Registered user",
      content: {
        "application/json": {
          schema: z.object({
            id: z.string(),
            name: z.string(),
            email: z.string().email(),
          }),
        },
      },
    },
  },
});

const app = new OpenAPIHono<AppBindings>({
    strict: false,
    defaultHook,
  });

app.openapi(routes.create, auth.handler(c.req.raw));

But I still have TS errors in the last line:

Argument of type 'Promise<Response>' is not assignable to parameter of type 'Handler<AppBindings, "/api/auth/**", {}, MaybePromise<TypedResponse<{ name: string; email: string; id: string; }, 200, "json" | "text">>>'.
(...)

Has anyone tried to glue this together yet?

Originally created by @Malin88 on GitHub (Nov 26, 2024). Original GitHub issue: https://github.com/better-auth/better-auth/issues/670 I'm trying to do it this way: ```JS const create = createRoute({ tags: ["auth"], method: "post", path: "/api/auth/**", request: { body: { content: { "application/json": { schema: { name: z.string(), email: z.string().email(), password: z.string(), }, }, }, description: "Register user", required: true, }, }, responses: { 200: { description: "Registered user", content: { "application/json": { schema: z.object({ id: z.string(), name: z.string(), email: z.string().email(), }), }, }, }, }, }); const app = new OpenAPIHono<AppBindings>({ strict: false, defaultHook, }); app.openapi(routes.create, auth.handler(c.req.raw)); ``` But I still have TS errors in the last line: ``` Argument of type 'Promise<Response>' is not assignable to parameter of type 'Handler<AppBindings, "/api/auth/**", {}, MaybePromise<TypedResponse<{ name: string; email: string; id: string; }, 200, "json" | "text">>>'. (...) ``` Has anyone tried to glue this together yet?
GiteaMirror added the locked label 2026-04-13 03:26:26 -05:00
Author
Owner

@Bekacru commented on GitHub (Nov 26, 2024):

You shouldn't do that. Instead, use the OpenAPI plugin provided by Better Auth. If you want to merge them, you can merge the objects instead.

<!-- gh-comment-id:2501920563 --> @Bekacru commented on GitHub (Nov 26, 2024): You shouldn't do that. Instead, use the OpenAPI plugin provided by Better Auth. If you want to merge them, you can merge the objects instead.
Author
Owner

@Malin88 commented on GitHub (Nov 27, 2024):

Thanks for the advice, is there a way I can somehow connect this to my endpoints? I also use the Scalar library and keep everything under /api/reference it would be nice to have the endpoints from auth as well

<!-- gh-comment-id:2503778236 --> @Malin88 commented on GitHub (Nov 27, 2024): Thanks for the advice, is there a way I can somehow connect this to my endpoints? I also use the Scalar library and keep everything under `/api/reference` it would be nice to have the endpoints from auth as well
Author
Owner

@ferkanzai commented on GitHub (Nov 28, 2024):

Hi, I'm working with the same stack and I'm not 100% sure how to achieve merging the 2 objects... How can I retrieve the schema object from better-auth OpenAPI plugin?

@Malin88 have you achieved a good result? Thanks in advance!

<!-- gh-comment-id:2505069059 --> @ferkanzai commented on GitHub (Nov 28, 2024): Hi, I'm working with the same stack and I'm not 100% sure how to achieve merging the 2 objects... How can I retrieve the schema object from better-auth OpenAPI plugin? @Malin88 have you achieved a good result? Thanks in advance!
Author
Owner

@Bekacru commented on GitHub (Nov 28, 2024):

you can get the generated schema. see the docs here:
https://www.better-auth.com/docs/plugins/open-api#generated-schema

I think you should be able to merge the objects with hono docs, but I'll have to look more if it requires more work around.

<!-- gh-comment-id:2505300652 --> @Bekacru commented on GitHub (Nov 28, 2024): you can get the generated schema. see the docs here: https://www.better-auth.com/docs/plugins/open-api#generated-schema I think you should be able to merge the objects with hono docs, but I'll have to look more if it requires more work around.
Author
Owner

@Malin88 commented on GitHub (Nov 28, 2024):

@Bekacru a small mistake in the documentation. This function in the code is called openAPIGenerator

<!-- gh-comment-id:2506561590 --> @Malin88 commented on GitHub (Nov 28, 2024): @Bekacru a small mistake in the documentation. This function in the code is called `openAPIGenerator`
Author
Owner

@Bekacru commented on GitHub (Nov 28, 2024):

@Bekacru a small mistake in the documentation. This function in the code is called openAPIGenerator

oh sorry about that. we should rename it to generateOpenAPISchema in the next release tho.

<!-- gh-comment-id:2506577670 --> @Bekacru commented on GitHub (Nov 28, 2024): > @Bekacru a small mistake in the documentation. This function in the code is called `openAPIGenerator` oh sorry about that. we should rename it to `generateOpenAPISchema` in the next release tho.
Author
Owner

@a-man-called-q commented on GitHub (Jan 8, 2025):

@Malin88 @ferkanzai @Bekacru

I wrote a feat request for the documentation update in #1153

Hope it helps

<!-- gh-comment-id:2576558487 --> @a-man-called-q commented on GitHub (Jan 8, 2025): @Malin88 @ferkanzai @Bekacru I wrote a feat request for the documentation update in #1153 Hope it helps
Author
Owner

@iamyuu commented on GitHub (May 12, 2025):

Hi @Bekacru, I'm working on this and already achive both schema, but the Better Auth generate tags as Default is there any option to change that? and option to add base path into the operation?

Image

<!-- gh-comment-id:2870322002 --> @iamyuu commented on GitHub (May 12, 2025): Hi @Bekacru, I'm working on this and already achive both schema, but the Better Auth generate tags as `Default` is there any option to change that? and option to add base path into the operation? ![Image](https://github.com/user-attachments/assets/e017ec5c-4ee6-4b51-aa19-33cee933311b)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#8369