taxonomy/lib/api-middlewares/with-authentication.ts
2022-11-08 08:35:26 +04:00

16 lines
483 B
TypeScript

import type { NextApiHandler, NextApiRequest, NextApiResponse } from "next"
import { unstable_getServerSession } from "next-auth/next"
import { authOptions } from "@/lib/auth"
export function withAuthentication(handler: NextApiHandler) {
return async function (req: NextApiRequest, res: NextApiResponse) {
const session = await unstable_getServerSession(req, res, authOptions)
if (!session) {
return res.status(403).end()
}
return handler(req, res)
}
}