Files
zerobyte/app/server/modules/auth/auth.middleware.ts
Nico 99932a8522 refactor: better-auth (#319)
* refactor: better-auth

* chore: pr feedback

* chore: lower + trim usernames in db
2026-01-07 22:36:20 +01:00

33 lines
661 B
TypeScript

import { createMiddleware } from "hono/factory";
import { auth } from "~/lib/auth";
declare module "hono" {
interface ContextVariableMap {
user: {
id: string;
username: string;
hasDownloadedResticPassword: boolean;
};
}
}
/**
* Middleware to require authentication
* Verifies the session cookie and attaches user to context
*/
export const requireAuth = createMiddleware(async (c, next) => {
const session = await auth.api.getSession({
headers: c.req.raw.headers,
});
const { user } = session ?? {};
if (!user) {
return c.json<unknown>({ message: "Invalid or expired session" }, 401);
}
c.set("user", user);
await next();
});