I have an auth middleware that I want to use it's logic to develop other middlewares (ie: adminMiddleware, receptionMiddleware ...)
// adminMiddleware.ts
const authMiddleware = async (
req: NextApiRequest,
res: NextApiResponse,
next: NextHandler
) => {
// logic to check authentication status
(req as NextApiRequestWithSession).session = obj;
next();
};
I wrote the code below but I don't think it would work or if it's the best solution:
const adminMiddleware = (
req: NextApiRequest,
res: NextApiResponse,
next: NextHandler
) =>
nc()
.use(authMiddleware, async (req: NextApiRequestWithSession, res, next) => {
// check if the auth user is an admin
return next();
}) as NextConnect<NextApiRequestWithUser, NextApiResponse>;
export default adminMiddleware;