title: Display specific information for logged in user in Express js + mongodb
body: <p>Ok, somehow this question hasn't been raised nor did someone make an explanation about this problem: How can logged in user - User a - display his specific user information? I'm using Express js and PUG template engine. </p>
  
  <p>A user - User A - enters user information in a form input, this input is stored in mongoDB and retrieved inside the form (e.g. value=user_info[name]) - that's DONE. However, how should this user data displayed only to - User A.</p>
  
  <p>I assume, this is somehow related to sessionId - similar to app.js: req.session.userId = user._id ??! - but I'm not clear about the procedure. I hope that someone is able to demystify this, and provide an executable action plan. </p>
  
  <p>Since this issue wasn't covered in any explanation / Tutorial etc. across the internet, it might be helpful for future readers as well. Thanks a lot in advance. </p>
  
  <p>index.js - routing / authentication</p>
  
  <pre><code>var express = require('express');
  var router = express.Router();
  var User = require('../models/user');
  var mid = require('../middleware');
  var passport = require('passport');
  
  
  // GET /dashboard
  router.get('/dashboard', mid.requiresLogin, function(req, res, next) {
    User.findById(req.session.userId)
        .exec(function (error, user) {
          if (error) {
            return next(error);
          } else {
            return res.render('dashboard', { title: 'Dashboard', name: user.username });
          }
        });
  });
  
  // GET /logout
  router.get('/logout', function(req, res, next){
    if (req.session) {
      // delete session object
      req.session.destroy(function(err){
        if(err){
          return next(err);
        } else {
          return res.redirect('/');
        }
      });
    }
  });
  
  // GET /login
  router.get('/login', mid.loggedOut, function(req, res, next) {
    return res.render('login', { title: 'Log In'});
  });
  
  // POST /login
  router.post('/login', function(req, res, next) {
    if (req.body.email &amp;&amp; req.body.password) {
      User.authenticate(req.body.email, req.body.password, function (error, user) {
        if (error || !user) {
          var err = new Error('Wrong email or password.');
          err.status = 401;
          return next(err);
        }  else {
          req.session.userId = user._id;
          return res.redirect('/dashboard');
        }
      });
    } else {
      var err = new Error('Email and password are required.');
      err.status = 401;
      return next(err);
    }
  });
  
  // GET /register
  router.get('/register', mid.loggedOut, function(req, res, next) {
    return res.render('register', { title: 'Sign Up' });
  });
  
  // POST /register
  router.post('/register', function(req, res, next) {
    if (req.body.username &amp;&amp;
      req.body.email &amp;&amp;
      req.body.password &amp;&amp;
      req.body.confirmPassword) {
  
        // confirm that user typed same password twice
        if (req.body.password !== req.body.confirmPassword) {
          var err = new Error('Passwords do not match.');
          err.status = 400;
          return next(err);
        }
  
        // create object with form input
        var userData = {
          username: req.body.username,
          email: req.body.email,
          password: req.body.password
        };
  
        // use schema's `create` method to insert document into Mongo
        User.create(userData, function (error, user) {
          if (error) {
            return next(error);
          } else {
            req.session.userId = user._id;
            return res.redirect('/dashboard');
          }
        });
  
      } else {
        var err = new Error('All fields required.');
        err.status = 400;
        return next(err);
      }
  });
  </code></pre>
  
  <p>app.js - HINT: some code is commented out and not relevant</p>
  
  <pre><code>var express = require('express');
  var bodyParser = require('body-parser');
  var mongoose = require('mongoose');
  var session = require('express-session')
  var MongoStore = require('connect-mongo')(session);
  var User = require('./models/user');
  var User_info = require('./models/user_info');
  var app = express();
  
  
  // mongodb connection
  mongoose.connect("mongodb://localhost:27017/socialempireclub");
  var db = mongoose.connection;
  
  // mongo error
  db.on('error', console.error.bind(console, 'connection error:'));
  
  // use sessions for tracking logins
  app.use(session({
    secret: 'treehouse loves you',
    resave: true,
    saveUninitialized: false,
    store: new MongoStore({
      mongooseConnection: db
    })
  }));
  
  // make user ID available in templates
  app.use(function (req, res, next){
    res.locals.currentUser = req.session.userId;
    next();
  });
  
  // parse incoming requests
  app.use(bodyParser.json());
  app.use(bodyParser.urlencoded({ extended: false }));
  
  // serve static files from /public
  app.use(express.static(__dirname + '/public'));
  
  // view engine setup
  app.set('view engine', 'pug');
  app.set('views', __dirname + '/views');
  
  // include routes
  var routes = require('./routes/index');
  app.use('/', routes);
  
  /*app.post('/new', function(req, res){
    new User_info({
      firstName: req.body.firstName,
      lastName: req.body.lastName,
      streetAddress: req.body.streetAddress,
      city: req.body.city,
      state: req.body.state,
      zip: req.body.zip,
      country: req.body.country
    }).save(function(err, User_info){
        if(err) res.json(err);
        else res.redirect('/profileretrieve');
    });
  });*/
  
  /*app.get('/profileretrieve', function(req, res){
      User_info.find({}, function(err, docs){
          if(err) res.json(err);
          else    res.render('profileretrieve', {user_infos: docs});
      });
  });*/
  
   /*app.get('/user_info/:id', function(req, res){
    User_info.find({_id: req.params.id}, function(err, docs){
      if(err) res.json(err);
      else res.render('show', {user_infos: docs[0]});
    });
  });*/
  
  // catch 404 and forward to error handler
  app.use(function(req, res, next) {
    var err = new Error('File Not Found');
    err.status = 404;
    next(err);
  });
  
  // error handler
  // define as the last app.use callback
  app.use(function(err, req, res, next) {
    res.status(err.status || 500);
    res.render('error', {
      message: err.message,
      error: {}
    });
  });
  
  // listen on port 3000
  app.listen(3000, function () {
    console.log('Express app listening on port 3000');
  });
  </code></pre>
  
  <p>user.js - Schema 1</p>
  
  <pre><code>var mongoose = require('mongoose');
  var bcrypt = require('bcrypt');
  var UserSchema = new mongoose.Schema({
    username: {
      type: String,
      unique: true,
      required: true,
      trim: true
    },
    email: {
      type: String,
      unique: true,
      required: true,
      trim: true
    },
    password: {
      type: String,
      required: true
    }
  });
  
  // authenticate input against database documents
  UserSchema.statics.authenticate = function(email, password, callback) {
    User.findOne({ email: email })
      .exec(function (error, user){
        if (error) {
          return callback(error);
        } else if ( !user ) {
          var err = new Error('User not found.');
          err.status = 401;
          return callback(err);
        }
        bcrypt.compare(password, user.password, function(error, result){
          if (result === true) {
            return callback(null, user);
          } else {
            return callback();
          }
        })
      });
  }
  // hash password before saving to database
  UserSchema.pre('save', function(next){
    var user = this;
    bcrypt.hash(user.password, 10, function(err, hash){
      if (err) {
        return next(err);
      }
      user.password = hash;
      next();
    })
  });
  var User = mongoose.model('User', UserSchema);
  module.exports = User;
  </code></pre>
  
  <p>user_info.js</p>
  
  <pre><code>var mongoose = require('mongoose');
  
  var UserinfoSchema = new mongoose.Schema({
    firstName: String,
    lastName: String,
    streetAddress: String,
    city: String,
    state: String,
    zip: String,
    country: String
  });
  
  var User_info = mongoose.model('User_info', UserinfoSchema);
  module.exports = User_info;
  </code></pre>