title: Connecting a different user using mongoClient connexion pool
body: <p>I 'm using a MongoClient connexion pool at the top of my app.js node.js back end, it works well:</p>
  
  <pre><code> var db;
      MongoClient.connect("mongodb://localhost:27017", function(err, client) {
          if (err) throw err;
          db = client.db('myDb');
          // Start the application after the database connection is ready
          app.listen(3000);
          console.log("Listening on port 3000");
      });
  
      app.post('/getEvaluations', function(req, res) {
          db.collection('evaluations').find().toArray(function(err, docs) {
              if (err) throw err;
              res.send(docs);
  
          });
      })
  </code></pre>
  
  <p>I want to build a html form, for  getting the login password, but how can I  change the MongoClient connexion with the new login password afterwards ?</p>
  
  <p>For example, as long as John logs in, he needs to have READ rights to be able to call the getEvaluations web service.</p>
  
  <p>Do I have to always make the connexion with no user , then only check the user session inside each of the web services ? </p>
  
  <p>Or Is it possible to change the pool connexion , each time a mongoDb user logs in ( he might only have READ rights)?<br>
  Something like this ? </p>
  
  <pre><code>MongoClient.connect("mongodb://john:passwd@localhost:27017", function(err, client) {
      if (err) throw err;
      db = client.db('myDb');
      // Start the application after the database connection is ready
      app.listen(3000);
      console.log("Listening on port 3000");
  });
  </code></pre>
  
  <p>For example, let's say "John" with the password "passwd" wants to log in, how could I do ? Is there any standard code somewhere using a connexion pool ? </p>
  
  <p>I also use express-session but dont understand how to use it with the connexion pool.</p>
  
  <p>I can't find a mongoDb auth code using the connexion pool. There are some moogoose examples but, it is not the same .</p>
  
  <p>This is the idea I've got : </p>
  
  <p>The user logs in via HTTP, then if the login is ok, so a session is created, but how could i check afterwards if the user only has READ rights during all of his session  ?</p>
  
  <p>This is a pseudo code, from a moongoose example, but this is not what i need, i'm using mongoClient.</p>
  
  <pre><code>app.get('/getAuth', function(req, res) {
      var email = req.param('email');
      var password = req.param('password');
  
      //authenticate input against database
          UserSchema.statics.authenticate = function (email, password, callback) {
            User.findOne({ email: email })
              .exec(function (err, user) {
                if (err) {
                  return callback(err)
                  } else if (!user) {
                  var err = new Error('User not found.');
                  err.status = 401;
                  return callback(err);
                }
                bcrypt.compare(password, user.password, function (err, result) {
                  if (result === true) {
                    return callback(null, user);
                  } else {
                    return callback();
                  }
                })
              });
          }
  })
  </code></pre>
  
  <p>If somebody has an example code, i would really appreciate it, I don't use moogoose , but mongoClient Thank you.</p>
  
  <p>or maybe, this auth web service would be better :</p>
  
  <pre><code>app.get('/getAuth', function(req, res) {
      var user = req.param('user');
      var password = req.param('password');
      db.authenticate(user, password, function(err, res) {
          // callback
      });
  })
  </code></pre>
  
  <p>but then, what should I do with the connexion pool ? How could I reload it ?
  I can't understand how to do.</p>
  
  <p><strong>EDIT 1:</strong> This is what I got for now : I've built a getAuth web service, with a pool connexion starting inside of it, is it the right way to do ?</p>
  
  <pre><code>var db;
      app.get('/getAuth', function(req, res) {
  
          var user = "jose";
          var password = "passwd";
  
          MongoClient.connect("mongodb://"+user+":"+password+"@localhost:27017", function(err, client) {
              if (err) {console.log(err)}
  
              else{
                  db = client.db('myDb');
                  // Start the application after the database connection is ready
                  console.log("correct log in");
              }
          });
  
      }) 
  </code></pre>
  
  <p>how could I use a session now?</p>
  
  <p><strong>EDIT 2 :</strong> Seems to be working code with express-session using this doc :
  <a href="https://codeforgeek.com/manage-session-using-node-js-express-4/" rel="nofollow noreferrer">https://codeforgeek.com/manage-session-using-node-js-express-4/</a></p>
  
  <pre><code>  //use sessions for tracking logins
  app.use(session({secret: 'ssshhhhh',saveUninitialized: true,resave: true}));
  sess ={}; 
  
  // ---------------------------------------------------------------------AUTH USER
      app.get('/getAuth', function(req, res) {
  
          var user = "jose";
          var password = "passwd";
  
          MongoClient.connect("mongodb://"+user+":"+password+"@localhost:27017", function(err, client) {
              if (err) {console.log(err)}
  
              else{
                  db = client.db('myDb');
  
  
                   sess = req.session;
                  /*
                  * Here we have assign the 'session' to 'sess'.
                  * Now we can create any number of session variable we want.
                  * in PHP we do as $_SESSION['var name'].
                  * Here we do like this.
                  */
                  sess.email = "jose.golo@gmail.com"; // equivalent to $_SESSION['email'] in PHP.
                  sess.username = "jose"; // equivalent to $_SESSION['username'] in PHP.
                  res.end('done');
  
  
              }
          });
  
      })
  </code></pre>
  
  <p>Then, in my getEvaluations web service, i 'm checking if sess contains an email, otherwize, it means that he's not logged in, so he can't access the evaluations node.js web service :</p>
  
  <pre><code> app.post('/getEvaluations', function(req, res) {
  
          if (!sess.hasOwnProperty("email")  ){
              console.log('not logged in ');
          }else{
              db.collection('evaluations').find().toArray(function(err, docs) {
                  if (err) throw err;
                  res.send(docs);
              });
          }
      })
  </code></pre>
  
  <p>If you have a better solution, please let me know . This app is front end Ionic 1 app .</p>
  
  <p><strong>Question :</strong>
  Is it a normal behavior :
  I get authenticated inside of firefox, so i can access my evaluations.
  Now, I go to chrome, on the same computer , i don't have to authenticate to view the evaluations ?
  Is it because it is a user Id based session ?</p>