I am using next-connect
to apply some express middleware to my next.js application. While trying to add csrf middleware, I encountered the following error:
res.redirect is not a function
This error only appears when I applied the middleware in getServerSideProps
, it works fine in my API endpoints.
This works for example:
// in /pages/api/test.ts
nc()
.use(db)
.use(initializedSession)
.use(csrfProtection)
.use(initializedPassport)
.use(initializedPassportSession)
.post(
passport.authenticate("local", {failureRedirect: "/login"}),
(req: NextApiRequest, res: NextApiResponse, next) => {
return res.redirect("/");
}
)
Does work, but applying the same in getServerSideProps
for a react page:
// In /pages/login.tsx
export const getServerSideProps: GetServerSideProps = async ({req, res}) => {
await nc()
.use(db)
.use(initializedSession)
.use(csrfProtection)
.use(initializedPassport)
.use(initializedPassportSession)
.run(req, res);
return {
props: {
csrfToken: req.csrfToken(),
},
};
};
Does not work and results in res
not having a redirect method.
I'm not quite sure about this behavior, but I'm sure I'm missing something silly. Any one an idea?