title: Manually create passport session for already logged in User
body: <p>I am using passport js for login registration process. Once a new person logs in a cookie is created (custom by me) on browser that includes DB unique key.</p>
  
  <p>So how the program works is:</p>
  
  <p>If a newly person logins a cookie is created by this function</p>
  
  <pre><code>function setCookie(cname,cvalue,exdays) {
      //alert("Cookie set on tedJavascript");
      var d = new Date();
      d.setTime(d.getTime() + (exdays*24*60*60*1000));
      var expires = "expires="+d.toUTCString();
      document.cookie = cname + "=" + cvalue + "; " + expires;
  }
  </code></pre>
  
  <p>then the details entered on login page is send to post function on server (passport js).
  <strong>Local strategy authentication</strong> code is given below(same follows for guest,fb etc)</p>
  
  <pre><code>passport.use('guest',new LocalStrategy(
  function(username,password,done){
         User.getUserBycookie(password, function(err, user){
         if(err) throw err;
  
          //if cookie present (checked in terms of password)
         if(user){
                      //Some code
                      user.save()
                          }
                          else{
                              console.log("some problem with updating guest   user by db id!");
                          }
                      });
  
                      return done(null, user);
          }
  
  //if cookie not present
  else {              
                      // some part of code for new user
  
                      var newUser = new User();
                      newUser.guest.cookie = newUser._id;
                      trophytable_id = newUser._id;
                      newUser.guest.name = username;
                      newUser.save(function(err){
                          if(err) throw err;
                          return done(null, newUser);
                      });
  
       }});}));
  </code></pre>
  
  <p>The passport js automatically authenticates the information using strategies local,guest ,facebook,twitter etc, if correct it creates <strong>session</strong>.</p>
  
  <p>But if a already logged in person enters site it first checks if a cookie is present on browser if its present it picks out the <strong>DB key</strong>.</p>
  
  <p>After the key is known it finds the details of user from database(mongodb) and directly skips the login page and redirects to main menu.  </p>
  
  <p>But the problem is it redirects to main menu but unable to create session that could create problem further So <strong>want to create session again which passport created automatically for a new user</strong>.</p>
  
  <p>This is ensure authenticated function</p>
  
  <pre><code>function ensureAuthenticated(req, res, next){
  console.log("Inside ensureAuthenticated...............");
  if(req.isAuthenticated()){
      console.log("Inside if part of ensureAuthenticated-------");
      console.log(req.cookies);
      return next();
  } else {
              console.log("Inside else part of ensureAuthenticated----------");
              console.log(req.cookies.user_id);
              if(isEmpty(req.cookies.user_id)){
                  console.log("user_id == undefined");
                  res.redirect('/users/login');
              }
              else{
                   console.log("user_id defined");
                   return next();
              }
          }
  </code></pre>
  
  <p>}</p>
  
  <p><strong>next()</strong> leads to:</p>
  
  <pre><code>router.get('/',ensureAuthenticated, function(req,res){
  console.log("Inside router get ensure authenticated--------------------");
  if(isEmpty(req.session.passport)){
      //**have to create new session**
  
      //**getting user details from database**
      User.getUserBydbid(req.cookies.user_id, function(err, user){
          if(err) throw err;
          if(user){
              if(!isEmpty(user.local)){
  
                 //**here i want to create session**
  
             }else if(!isEmpty(user.guest)){
  
                      //**here i want to create session**
  
                  }
                   //skipped fb,twitter code
          }                   
          else console.log("*********some problem in finding     getUserBydbid");
      });
  
       //after creating session want to redirect to main menu 
       //it works but session not created
      //res.render('index',{userid:req.cookies.user_id});
  }
  else{
      //for newly logged in user
      res.render('index',{userid:req.session.passport.user});
  }});
  </code></pre>
  
  <p><strong>How can i manually create passport session for already logged in user?</strong></p>