title: How to create an admin and allow access to admin panel in node.js?
body: <p>I am very new to coding and am writing a personal project using node.js, express, mongoDB, and mongoose. I wrote most of it myself, however I hired someone to help me with the more advanced parts. I have lost contact with him and went back under the hood to create an admin panel I could use to write blog posts and other things. I am trying to write a middleware that only allows myself access to the route. However it is not working.</p>
  
  <pre><code>function adminAuth(req, res, next){
        if(req.user.isAdmin){
          return next();
        } else {
          res.redirect("/");
        }
      }
  </code></pre>
  
  <p>I am a bit confused of the syntax he has used to create a user schema and I am not sure how to add this isAdmin key value pair. Any help updating my users with an isAdmin key value would be extremely appreciated, and also helping me finish the middleware as (req.user.isAdmin) is not working! (If I do not provide the necessary code, please excuse my inexperience and tell me what you would like to see).</p>
  
  <p>Here is the Auth route the coder I hired wrote that I am having trouble deciphering how to pass in new data to the user model.</p>
  
  <pre><code>const isAdmin = false;
  
        const passwordHash = await bcrypt.hash(req.body.password, saltRounds);
  
        const db = client.db(dbName);
        const col = db.collection('users');
        const user = {
          email, firstName, lastName, password: passwordHash, isAdmin,
        };
  </code></pre>
  
  <p>local strategy</p>
  
  <pre><code>module.exports = function localStrategy() {
  
  
  passport.use(new Strategy(
      {
        usernameField: 'email',
        passwordField: 'password',
        passReqToCallback: true
      }, (req, email, password, done) =&gt; {
        const url = process.env.MONGOLAB_URI;
        const dbName = 'giftgrab';
  
        (async function addUser() {
          let client;
      try {
        client = await MongoClient.connect(url);
  
        const db = client.db(dbName);
        const col = db.collection('users');
  
        const user = await col.findOne({ email });
        debug('Found user by email');
        debug(user);
        if (!user) {
          req.flash('error', 'The username or password is wrong');
          done(null, false);
        } else {
          const match = await bcrypt.compare(password, user.password);
  
          if (match) {
            done(null, user);
          } else {
            req.flash('error', 'The username or password is wrong');
            // we pass null because it did not error, just failed
            done(null, false);
          }
        }
      } catch (e) {
        debug(e.stack);
      }
  
      client.close();
    }());
  }
  </code></pre>