title: Hot to set authentication in passport-jwt with different role of user?
body: <p>I'm trying to add a role called admin to authenticate admins logged into dashboard web app while the normal user can just access the regular pages.</p>
  
  <p>For a normal user, I require the passport in <code>server.js</code> like</p>
  
  <pre><code>// use passport 
  app.use(passport.initialize());
  require("./config/passport")(passport);
  </code></pre>
  
  <p>In the <code>config/passport.js</code>, like the code in official example, I try this:</p>
  
  <pre><code>const JwtStrategy = require('passport-jwt').Strategy,
  ExtractJwt = require('passport-jwt').ExtractJwt;
  const mongoose = require('mongoose');
  const User = mongoose.model("users");
  const key  =require("../config/key");
  
  const  opts = {};
  opts.jwtFromRequest = ExtractJwt.fromAuthHeaderAsBearerToken();
  opts.secretOrKey = key.secretKey;
  
  module.exports = passport =&gt; {
      passport.use(new JwtStrategy(opts, (jwt_payload, done) =&gt; {
          // console.log(jwt_payload);
          User.findById(jwt_payload.id)
              .then(user =&gt; {
                  if(user) {
                      return done(null, user);
                  }
  
                  return done(null, false);
              })
              .catch(err =&gt; console.log(err));
      }));
  };
  </code></pre>
  
  <p>This way works fine, and I use them in the route</p>
  
  <pre><code>router.get("/current", passport.authenticate("jwt", {session: false}), (req, res) =&gt; {
      res.json({
          id: req.user.id,
          name: req.user.name,
          username: req.user.username,
          email: req.user.email,
          avatar: req.user.avatar,
      });
  }) 
  </code></pre>
  
  <p>However, while I'm adding a role in the token rule:</p>
  
  <pre><code>const rule = {id:admin.id, email: admin.email, avatar: admin.avatar, admin: admin.admin};
  </code></pre>
  
  <p>How could I check if the admin property is true to query different Collections in <code>passport.js</code></p>
  
  <p>I tried this, which doesn't work for me with the error seems like the server run twice:</p>
  
  <pre><code>module.exports = passport =&gt; {
  passport.use(new JwtStrategy(opts, (jwt_payload, done) =&gt; {
      // console.log(jwt_payload);
      if(jwt_payload.admin){
          Admin.findById(jwt_payload.id)
          .then(user =&gt; {
              if(user) {
                  return done(null, user);
              }
  
              return done(null, false);
          })
          .catch(err =&gt; console.log(err));
      } else {
      User.findById(jwt_payload.id)
          .then(user =&gt; {
              if(user) {
                  return done(null, user);
              }
  
              return done(null, false);
          })
          .catch(err =&gt; console.log(err));
      }
  }));};
  </code></pre>
  
  <p>The error is : 
  <a href="https://i.stack.imgur.com/8Yyji.png" rel="nofollow noreferrer">Error</a></p>