title: Using next-iron-session's "withIronSession" with Next.JS to perform simple authentication
body: <p>So I have a very simple Next.js application that I would like to add authentication to using next-iron-session and the withIronSession function. Inside of my api folder, I have a simple component that grabs the login form data, performs a complex authentication action, then sets the user using</p>
  <p>req.session.set(&quot;user&quot;, { email });</p>
  <pre><code>import { withIronSession } from &quot;next-iron-session&quot;;
  
  const VALID_EMAIL = &quot;user&quot;;
  const VALID_PASSWORD = &quot;password&quot;;
  
  export default withIronSession(
    async (req, res) =&gt; {
      if (req.method === &quot;POST&quot;) {
        const { email, password } = req.body;
  
        if (email === VALID_EMAIL &amp;&amp; password === VALID_PASSWORD) {
          req.session.set(&quot;user&quot;, { email });
          await req.session.save();
          console.log(req.session.get());
          return res.status(201).send(&quot;&quot;);
        }
  
        return res.status(403).send(&quot;&quot;);
      }
  
      return res.status(404).send(&quot;&quot;);
    },
    {
      cookieName: &quot;MYSITECOOKIE&quot;,
      password: '2gyZ3GDw3LHZQKDhPmPDL3sjREVRXPr8'
    }
  );
  </code></pre>
  <p>After the user is set in the session, I return a 201, and the client redirects to my protected page, where I try to grab that user in the getServerSideProps function using the withIronSession as a wrapper.</p>
  <pre><code>import { withIronSession } from &quot;next-iron-session&quot;;
  
  export const getServerSideProps = withIronSession(
      async ({ req, res }) =&gt; {
        const user = req.session.get(&quot;user&quot;);
  
        console.log(user);
    
        if (!user) {
          res.statusCode = 403;
          res.end();
          return { props: {} };
        }
    
        return {
          props: { }
        };
      },
      {
          cookieName: 'MYSITECOOKIE',
          password: &quot;2gyZ3GDw3LHZQKDhPmPDL3sjREVRXPr8&quot;
      }
    );
  </code></pre>
  <p>However, even though the user is found in the API function after it is set, ({&quot;user&quot;, { email }}), that same session object returns {} in the getServerSideProps function in my protected component, which in my case always results in a 403. Is there a way to access the user that is set in the login component in the getServerSideProps function?</p>
  <p>Note*
  I was trying to follow this article, maybe it provides more information.</p>
  <p><a href="https://dev.to/chrsgrrtt/easy-user-authentication-with-next-js-18oe" rel="nofollow noreferrer">https://dev.to/chrsgrrtt/easy-user-authentication-with-next-js-18oe</a></p>