2022-10-26 13:18:06 +00:00
|
|
|
import type { NextApiHandler, NextApiRequest, NextApiResponse } from "next"
|
2022-11-08 04:35:26 +00:00
|
|
|
import { unstable_getServerSession } from "next-auth/next"
|
2022-11-07 12:37:47 +00:00
|
|
|
|
|
|
|
|
import { authOptions } from "@/lib/auth"
|
2022-10-26 13:18:06 +00:00
|
|
|
|
|
|
|
|
export function withAuthentication(handler: NextApiHandler) {
|
|
|
|
|
return async function (req: NextApiRequest, res: NextApiResponse) {
|
2022-11-07 12:37:47 +00:00
|
|
|
const session = await unstable_getServerSession(req, res, authOptions)
|
2022-10-26 13:18:06 +00:00
|
|
|
|
|
|
|
|
if (!session) {
|
|
|
|
|
return res.status(403).end()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return handler(req, res)
|
|
|
|
|
}
|
|
|
|
|
}
|