title: Passport with express and nextjs
body: <p>I'm implementing a login using NextJS, Express and Passport. I've setup Passport and when I try to use the function req.isAuthenticated() its says it's not defined.</p>
  <pre><code>  server.get(&quot;/profilePage&quot;, (res, req) =&gt; {
      if (req.isAuthenticated()) {
         if (req.user.accountType === &quot;user&quot;) {
  </code></pre>
  <p>If I use this this solution (<a href="https://stackoverflow.com/questions/38820251/how-is-req-isauthenticated-in-passport-js-implemented">How is req.isAuthenticated() in Passport JS implemented?</a>) I'm not able to get data from the req</p>
  <p>How I'm able to solve this?</p>
  <p>EDIT:</p>
  <pre><code>require(&quot;dotenv&quot;).config();
  const { log } = require(&quot;console&quot;);
  //new branch
  //Express &amp; Passport
  const express = require(&quot;express&quot;),
    bodyParser = require(&quot;body-parser&quot;),
    session = require(&quot;express-session&quot;),
    passport = require(&quot;passport&quot;),
    bcrypt = require(&quot;bcrypt-nodejs&quot;),
    fs = require(&quot;fs&quot;),
    { createServer } = require(&quot;https&quot;);
  
  require(&quot;./server/config/passport.js&quot;)(passport);
  
  //Next
  const next = require(&quot;next&quot;);
  
  const dev = process.env.NODE_ENV !== &quot;production&quot;;
  const app = next({ dev });
  const handle = app.getRequestHandler();
  
  //AWS
  const aws = require(&quot;aws-sdk&quot;),
    dd = new aws.DynamoDB(),
    tableName = &quot;UsersDB&quot;;
  
  const port = process.env.PORT || 3000;
  
  app.prepare().then(() =&gt; {
    const server = express();
    server.use(bodyParser.urlencoded({ extended: true }));
    server.use(bodyParser.json());
  
    server.use(
      session({
        secret: process.env.SESSION_SECRET,
        resave: false,
        saveUninitialized: false,
      })
    );
  
    server.use(passport.initialize());
    server.use(passport.session());
  
    //HomePage
    server.get(&quot;/&quot;, (req, res) =&gt; {
      return app.render(req, res, &quot;/index&quot;, req.query);
    });
  
    //Login
    server.get(&quot;/login&quot;, (req, res) =&gt; {
      return app.render(req, res, &quot;/testLogin&quot;, req.query);
    });
  
    server.post(
      &quot;/login&quot;,
      passport.authenticate(&quot;local-login&quot;, {
        successRedirect: &quot;/profilePage&quot;,
        failureRedirect: &quot;/login&quot;,
      })
    );
  
  server.get(&quot;/profilePage&quot;, (res, req) =&gt; {
      console.log(req.user.username)
      if (req.isAuthenticated()) {
         if (req.user.accountType === &quot;user&quot;) {
          res.redirect(&quot;/test&quot;); 
      } else {
        return res.redirect(&quot;/testProfileReciveData&quot;);
      }
    }
    });
  </code></pre>
  <p>config/passport if from here: <a href="https://github.com/christianboutin/passportjs-dynamodb/blob/master/config/passport.js" rel="nofollow noreferrer">https://github.com/christianboutin/passportjs-dynamodb/blob/master/config/passport.js</a></p>
  <p>EDIT2: I think the main issue is that I cannot read req.user. When I try to console log req I get and object, but when I try to read req.user is says undefined. I check req and there is a field named user.</p>