The withCsrf
function is used to protect API endpoints from CSRF attacks. It will return a 403
error if the CSRF token is invalid.
import { NextApiRequest,NextApiResponse } from "next";
import { withAuthedUser } from '~/core/middleware/with-authed-user';
import { withPipe } from '~/core/middleware/with-pipe';
import { withExceptionFilter } from '~/core/middleware/with-exception-filter';
import { withCsrf } from '~/core/middleware/with-csrf';
export default function owner(req: NextApiRequest, res: NextApiResponse) {
const handler = withPipe(
withCsrf(),
withAuthedUser,
(req, res) => {
res.status(200).json({ message: 'Hello World!' });
}
);
return withExceptionFilter(req, res)(handler);
}