kstulgys
kstulgys5mo ago

can I use convex auth with hono?

There is no such example convex auth and hono.
9 Replies
Michal Srb
Michal Srb5mo ago
Authorization - Convex Auth
Authentication library for your Convex backend
kstulgys
kstulgysOP5mo ago
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.
Michal Srb
Michal Srb5mo ago
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);
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?
cameronm
cameronm5mo ago
@Michal Srb 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)
Michal Srb
Michal Srb5mo ago
@cameronm try to typecast c.req as any
Michal Srb
Michal Srb5mo ago
There's also a different method for mounting the hono router inside the normal convex router: https://stack.convex.dev/hono-with-convex#extending-routes-using-the--prefix
Advanced HTTP Endpoints: Convex ❤️ Hono
Adding advanced HTTP Endpoint functionality by extending Convex with Hono.
cameronm
cameronm5mo ago
@Michal Srb 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?
Michal Srb
Michal Srb5mo ago
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)
cameronm
cameronm5mo ago
Ah cool, ok. I'll play around with both approaches to see which mental model works better for me lol. Thanks for your help!

Did you find this page helpful?