How to use const session = c.get("session") and const user = c.get("user") in Hono when routes?

Hi everyone, I'm using Hono but the only way I can retrieve c.get("user") is if I export my app variables type? Is there a more elegant way to do this? Maybe export the "whole app" in each route instead of just variables? index.ts:
// Define app variables type to be used across routes
export type AppVariables = {
user: typeof auth.$Infer.Session.user | null;
session: typeof auth.$Infer.Session.session | null;
};

// Initialize Hono app
const app = new Hono<{
Variables: AppVariables;
}>();

// Auth routes with Better-Auth
app.on(["POST", "GET"], "/api/auth/*", (c) => {
return auth.handler(c.req.raw);
});

// Middlewares
app.use("/api/auth/*", corsMiddleware);
app.use("*", sessionMiddleware, logger(), prettyJSON());

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const routes = app.basePath("/api").route("/threads", threads);
export type AppType = typeof routes;

// Serve static files
app.get("*", serveStatic({ root: "./frontend/dist" }));
app.get("*", serveStatic({ path: "./frontend/dist/index.html" }));

// Export the app with server configuration
export default {
port: process.env["PORT"] || 3000,
hostname: "0.0.0.0",
fetch: app.fetch,
};
// Define app variables type to be used across routes
export type AppVariables = {
user: typeof auth.$Infer.Session.user | null;
session: typeof auth.$Infer.Session.session | null;
};

// Initialize Hono app
const app = new Hono<{
Variables: AppVariables;
}>();

// Auth routes with Better-Auth
app.on(["POST", "GET"], "/api/auth/*", (c) => {
return auth.handler(c.req.raw);
});

// Middlewares
app.use("/api/auth/*", corsMiddleware);
app.use("*", sessionMiddleware, logger(), prettyJSON());

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const routes = app.basePath("/api").route("/threads", threads);
export type AppType = typeof routes;

// Serve static files
app.get("*", serveStatic({ root: "./frontend/dist" }));
app.get("*", serveStatic({ path: "./frontend/dist/index.html" }));

// Export the app with server configuration
export default {
port: process.env["PORT"] || 3000,
hostname: "0.0.0.0",
fetch: app.fetch,
};
example of a route (thread.ts):
const app = new Hono<{ Variables: AppVariables }>()
.use("/*", rateLimitMiddleware)
.post("/", zValidator("json", searchRequestSchema), async (c) => {
const user = c.get("user");

if (!user) {
return c.json({ error: "Unauthorized" }, 401);
}

...
const app = new Hono<{ Variables: AppVariables }>()
.use("/*", rateLimitMiddleware)
.post("/", zValidator("json", searchRequestSchema), async (c) => {
const user = c.get("user");

if (!user) {
return c.json({ error: "Unauthorized" }, 401);
}

...
9 Replies
Blank
Blank5mo ago
you can try c.var.user or are you asking about something different?
MarvinKR
MarvinKROP5mo ago
I’m wondering if there is a better way than having to import variables in each individual route?
Blank
Blank5mo ago
create a utility function
export const createRouter = () => new Hono<{...}>();
export const createRouter = () => new Hono<{...}>();
and call this
MarvinKR
MarvinKROP5mo ago
Yeah that’s what I thought, thx!
Blank
Blank5mo ago
its more of to do with how hono works than better-auth
MarvinKR
MarvinKROP5mo ago
True sorry wrong discord I guess haha
MarvinKR
MarvinKROP5mo ago
doing this actually breaks my Hono RPC setup?
No description
MarvinKR
MarvinKROP5mo ago
Think i'll stick to variables for the moment, thx
Blank
Blank5mo ago
oh that might happen yeah, i havent used hono rpc much

Did you find this page helpful?