title: Cannot access session or req.user in getServerSideProps in nextjs
body: <p>I need to fetch data based on a <code>userId</code> but using SSR and utilizing the SSR features, how can I access the <code>userId</code> obtained after the login is successful in the SSR function <code>getServerSideProps</code> provided by NextJS. I have the following implementation right now.</p>
  <p>User is redirected to <code>pages/index.js</code> once he logs in,
  This is <code>pages/index.js</code></p>
  <pre><code>import { useUser } from '../libs/hooks';
  import { client } from '../middlewares/database';
  import Home from './Home';
  import Landing from './Landing';
  
  const App = ({ data }) =&gt; {
    
      const [user] = useUser();
      /* I have the user details here and I have a hook for getting user details,
  but this is the client-side */
      return user?&lt;Home data={data} /&gt;:&lt;Landing /&gt;
  }
  
  export async function getServerSideProps(context) {
      const userId = context.req.headers.userId;
      // userId is undefined here, I need the userId here
      if (!client.isConnected()) await client.connect();
  
      const db = client.db(process.env.DB_NAME);
      const response = await db.collection(&quot;data&quot;).find({ userId });
      
      return { props: { data: response } }
  }
  
  export default App;
  
  </code></pre>
  <p>I am using <code>passport.js</code> and <code>next-session</code> to authenticate the user and create a session <code>sid</code> which is available in all functions.  If I can store the <code>userId</code> in the session that would help as well.</p>
  <p>This is my server middleware.</p>
  <pre><code>import nextConnect from &quot;next-connect&quot;;
  import passport from &quot;../libs/passport&quot;;
  import database from &quot;./database&quot;;
  import session from &quot;./session&quot;;
  
  const middleware = nextConnect();
  
  middleware
    .use(database)
    .use(session)
    .use(passport.initialize())
    .use(passport.session());
  
  export default middleware;
  </code></pre>
  <p>And this is how the login auth file looks;
  <code>auth.js</code></p>
  <pre><code>import nextConnect from 'next-connect';
  import { session } from 'next-session';
  import { extractUser } from '../../libs/api-helpers';
  import passport from '../../libs/passport';
  import middleware from '../../middlewares/middleware';
  
  const handler = nextConnect();
  handler.use(middleware);
  
  handler.post(passport.authenticate('local'), async (req, res) =&gt; {
  
    res.json({ user: extractUser(req) });
  });
  </code></pre>
  <p>I can either use the cookie approach or the session approach. How do I achieve that here in this scenario?</p>