Next.js Discord

Discord Forum

Setting custom request objects

Unanswered
Giant panda posted this in #help-forum
Open in Discord
Giant pandaOP
I was wondering if this is a sin, or even a security issue. I want to add an object to the context request body within getInitialProps(), is that a good, brilliant or terrible idea?

MyApp.getInitialProps = async ({ ctx }) => {
  const jwtCookie = getCookie(ctx.req, 'jwt');
  if (jwtCookie) {
    try {
      ctx.req.user = jwt.verify(jwtCookie, process.env.JWT_SECRET);
     } catch (err) {
       removeCookie(ctx.res, 'jwt');
     }
  }
  return {
    themeColor: 'dark',
  };
};


Why do I want this?
-I want to access the element via getServerSideProps and do if(ctx.req.user === undefined) return {notFound: true}
Why don't I re-validate the JWT?
-My mind was filled with the idea of: reducing repetition, potentially improving performance

What are your thoughts on this?

0 Replies