title: How to check if user is logged in with node.js and mongoose
body: <p>So I want let's say p.x on /profile I want to know if the user is logged in with a command!</p>
  
  <p>So when he goes to home page "/" it's going to render either the profile.ejs if he is logged in or the home.ejs to register and log in.But I don't when do that with app.get('/login'... because I need it again when I am already using the profile to redirect to another page but he has to be logged in to go there.</p>
  
  <p>Here is code:</p>
  
  <pre><code>if (process.env.NODE_ENV !== 'production') {
    require('dotenv').config()
  }
  
  const express = require('express')
  const app = express()
  const bcrypt = require('bcrypt')
  const passport = require('passport')
  const flash = require('express-flash')
  const session = require('express-session')
  const methodOverride = require('method-override')
  const initializePassport = require('./passport-config')
  var mongodb = require('mongodb');
  const bodyParser = require('body-parser');
  initializePassport(
    passport,
    email =&gt; users.find(user =&gt; user.email === email),
    id =&gt; users.find(user =&gt; user.id === id)
  )
  
  var urlencodedParser = bodyParser.urlencoded({extended:false})
  var MongoClient = require('mongodb').MongoClient;
  var url = "mongodb://localhost:27017/";
  const users = []
  
  var http = require('http');
  
  /* ************ */
  app.use(express.static(__dirname + '/public'));
  app.set('view-engine', 'ejs')
  app.use(express.urlencoded({ extended: false }))
  app.use(flash())
  app.use(session({
    secret: process.env.SESSION_SECRET,
    resave: false,
    saveUninitialized: false
  }))
  app.use(passport.initialize())
  app.use(passport.session())
  app.use(methodOverride('_method'))
  
  
  app.use( bodyParser.json() );     
  
  var dbConn = mongodb.MongoClient.connect('mongodb://localhost:27017/');
  
  var path = require('path');
  
  const mongoose = require('mongoose');
  mongoose.connect('mongodb://localhost/nyg', {useNewUrlParser: true});
  const Schema = mongoose.Schema;
  
  const userSchema = new Schema({
    email: String,
    password: String
  });
  
  const User = mongoose.model('users', userSchema);
  
  app.post('/register', async (req, res, next) =&gt; {
    const user = await User.findOne({
      email: req.body.email,
      password: req.body.password
    }).exec();
  
    if (user) {
      res.redirect('/register');
      next();
    }
  
    else new User({
      email: req.body.email,
      password: req.body.password
    })
      .save()
      .exec();
    res.redirect('/login');
    next();
  });
  
  app.post('/login', async (req, res, next) =&gt; {
    const user = await User.findOne({
      email: req.body.email,
      password: req.body.password
    });
    if (user) res.redirect('/');
    else res.redirect('/login');
    next();
  });
  
  
  app.get('/', (req, res) =&gt; {
    if (req.isAuthenticated()) res.render('profile.ejs')
  
    else res.render('index.ejs')
  })
  
  app.get('/login', (req, res) =&gt; {
    res.render('login.ejs')
  })
  
  
  app.get('/register', (req, res) =&gt; {
    res.render('register.ejs')
  })
  
  
  app.delete('/logout', (req, res) =&gt; {
    req.logOut()
    res.redirect('/login')
  })
  
  
  
  app.listen(process.env.PORT || 3000, process.env.IP || '0.0.0.0' );
  
  </code></pre>