var express     = require("express"),
    router      = express.Router(),
    passport    = require("passport"),
    User        = require("../models/User");


router.get("/register", function(req, res) {
   res.render("register") ;
});

router.post("/register", function(req, res) {
    var newUser = new User({username: req.body.username});
    User.register(newUser, req.body.password, function(err, user){
        if(err){
            req.flash("error", err.message);
            console.log(err);
            res.redirect("/register");
        }
        user.starScore = 0;
        user.list.push({title: "Make a List!", starValue: 1});
        user.save();
        passport.authenticate("local")(req, res, function(){
            req.flash("success", "Welcome to YelpCamp " + user.username + "!");
            res.redirect("/");
        });
    });
});

//show login in form
router.get("/login", function(req, res) {
    res.render("login");
});

// handles login logic
router.post("/login", passport.authenticate("local",
     {  successRedirect: "/",
        successFlash: "Welcome!",
        failureRedirect: "/login",
        failureFlash: true
     }), function(req, res) {
});

// logout route
router.get("/logout", function(req, res) {
    req.logout();
    req.flash("success", "You are logged out!");
    res.redirect("/");
});

module.exports = router;
@@ -543,17 +543,28 @@
        %7D
-%0A
+ else %7B%0A    
         
@@ -587,24 +587,28 @@
  0;%0A        
+  
+  
 user.list.pu
@@ -646,24 +646,28 @@
 Value: 1%7D);%0A
+    
         user
@@ -679,24 +679,28 @@
 ();%0A        
+    
 passport.aut
@@ -744,32 +744,36 @@
 ()%7B%0A            
+  
+  
 req.flash(%22succe
@@ -828,32 +828,36 @@
 %22);%0A            
+    
 res.redirect(%22/%22
@@ -855,17 +855,34 @@
 irect(%22/
-%22
+user/%22 + user._id 
 );%0A     
@@ -884,20 +884,34 @@
         
+  
+  
 %7D);%0A
+        %7D%0A
     %7D);%0A
@@ -917,16 +917,17 @@
 %0A%7D);%0A%0A//
+ 
 show log
@@ -1009,16 +1009,724 @@
 );%0A%7D);%0A%0A
+router.post('/login', function(req, res, next) %7B%0A  passport.authenticate('local', function(err, user, info) %7B%0A    if (err) %7B%0A        req.flash(%22error%22, info.message);%0A        return next(err);%0A    %7D%0A    if (!user) %7B %0A        req.flash(%22error%22, info.message);%0A        return res.redirect('/login');%0A    %7D else %7B%0A        req.logIn(user, function(err) %7B%0A            if (err) %7B %0A                req.flash(%22error%22, info.message);%0A                return next(err); %0A            %7D else %7B%0A                req.flash(%22success%22, %22Welcome back %22 + user.username + %22! Let's get get something done!%22);%0A                return res.redirect('/user/' + user._id);%0A            %7D%0A        %7D);%0A    %7D%0A  %7D)(req, res, next);%0A%7D);%0A%0A// 
 // handl
@@ -1740,16 +1740,19 @@
 n logic%0A
+// 
 router.p
@@ -1796,16 +1796,19 @@
 local%22,%0A
+// 
      %7B  
@@ -1829,16 +1829,19 @@
 t: %22/%22,%0A
+// 
         
@@ -1866,16 +1866,19 @@
 come!%22,%0A
+// 
         
@@ -1904,16 +1904,19 @@
 login%22,%0A
+// 
         
@@ -1934,16 +1934,19 @@
 h: true%0A
+// 
      %7D),
@@ -1967,16 +1967,19 @@
  res) %7B%0A
+// 
 %7D);%0A%0A// 

refactor login route