mirror of
https://github.com/mosh-hamedani/helpdesk.git
synced 2026-05-21 11:58:19 +02:00
24 lines
566 B
TypeScript
24 lines
566 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;
|
|
}
|
|
|
|
if (session.user.deletedAt) {
|
|
res.status(401).json({ error: "Unauthorized" });
|
|
return;
|
|
}
|
|
|
|
req.user = session.user;
|
|
req.session = session.session;
|
|
next();
|
|
};
|