#can I use convex auth with hono?

1 messages · Page 1 of 1 (latest)

dense light
#

There is no such example convex auth and hono.

frosty scroll
dense light
#

have not tried this yet but I want to create some http endpoints that do not care about the auth and I want to use hono as it has nice interface to parsing urls etc, but I could not manage to make it work when I have auth setup in my http.ts file.

frosty scroll
#

Hmm, the hono router doesn't layer very well with the built-in router.

Some form of this should get you to a working setup, we'll need more time to think about how to ideally combine these:

import { httpRouter } from "convex/server";
import { HonoWithConvex, HttpRouterWithHono } from "convex-helpers/server/hono";
import { Hono } from "hono";
import { auth } from "./auth";
import { ActionCtx } from "./_generated/server";

const http = httpRouter();

auth.addHttpRoutes(http);


const app: HonoWithConvex<ActionCtx> = new Hono();

app.get("/.well-known/openid-configuration", async (c) => {
  const [httpAction] = http.lookup("/.well-known/openid-configuration", "GET")
  return httpAction(c.env, c.req);
});

app.get("/.well-known/jwks.json", async (c) => {
  const [httpAction] = http.lookup("/.well-known/jwks.json", "GET")
  return httpAction(c.env, c.req);
});


app.get("/api/auth/signin/*", async (c) => {
  const [httpAction] = http.lookup("/api/auth/signin/foo", "GET")
  return httpAction(c.env, c.req);
});

app.on(['GET', 'POST'], "/api/auth/callback/*", async (c) => {
  const [httpAction] = http.lookup("/api/auth/callback/foo", c.req.method);
  return httpAction(c.env, c.req);
});

export default new HttpRouterWithHono(app);

I didn't test this, or even typechecked it, but maybe you can make it work?

dim nova
# frosty scroll Hmm, the hono router doesn't layer very well with the built-in router. Some for...

@frosty scroll Any more thoughts on this? I can get everything with hono working great, but the /.well-known/openid-configuration and /.well-known/jwks.json paths from convex auth routes don't play well and always return null for the user

I did a test of the above as well and there is a type error on c.req when calling httpAction

Argument of type 'HonoRequest<"/.well-known/jwks.json", unknown>' is not assignable to parameter of type 'Request'.
Type 'HonoRequest<"/.well-known/jwks.json", unknown>' is missing the following properties from type 'Request': cache, credentials, destination, headers, and 10 more.ts(2345)

frosty scroll
#

@dim nova try to typecast c.req as any

dim nova
#

@frosty scroll Actually the above code you shared works to ensure the auth routes resolve correctly with the hono router 😀 .

RE that article. So if I am understanding the second method in that article correctly, we are basically passing any incoming http requests to the hono router with the action context?

frosty scroll
#

Yeah. It's a flipped approach. In the code I shared, we ask the hono router to route the specific routes to the built-in router.

In the article, we ask the built-in router to route the other routes to the hono router. (ofc you'd need to add the logic for not routing the auth routes to hono)

dim nova
#

Ah cool, ok. I'll play around with both approaches to see which mental model works better for me lol. Thanks for your help!

safe ibex
#

The guide referenced above is really useful for the client piece of HTTP actions but I'm wondering why there isn't anything there on the recommended way of parsing that header and checking for the authenticated state. Wondering if something like a customHttpAction would be useful to define a helper like authenticatedAction but for http actions. Since Michal is not active afaik, I think maybe @grizzled lotus or @tame gyro can help? 🙏

tame gyro
#

Could you add hono middleware, if you're already using hono?

safe ibex
#

Not actually using hono, so was looking more for something like a customHttpAction, but i can easily switch to hono I guess as well!