mirror of
https://github.com/mosh-hamedani/helpdesk.git
synced 2026-05-21 11:58:19 +02:00
19 lines
464 B
TypeScript
19 lines
464 B
TypeScript
|
|
import type { RequestHandler } from "express";
|
||
|
|
import { fromNodeHeaders } from "better-auth/node";
|
||
|
|
import { auth } from "../lib/auth";
|
||
|
|
|
||
|
|
export const requireAuth: RequestHandler = async (req, res, next) => {
|
||
|
|
const session = await auth.api.getSession({
|
||
|
|
headers: fromNodeHeaders(req.headers),
|
||
|
|
});
|
||
|
|
|
||
|
|
if (!session) {
|
||
|
|
res.status(401).json({ error: "Unauthorized" });
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
req.user = session.user;
|
||
|
|
req.session = session.session;
|
||
|
|
next();
|
||
|
|
};
|