not getting expected result while using passport in nodejs for authentication
in server.js :
var User = mongoose.model('User'); passport.use(new LocalStrategy( function(username,password,done){
User.findOne({username:username}).exec(function(err,user){
if(user){
return done(null,user);
} else{
return done(null,false);
}
});
}
));
passport.serializeUser(function(user,done){
if(user){
done(null,user._id);
} });
passport.deserializeUser(function(id,done){
User.findOne({_id : id}).exec(function(err,user){
if(user){
return done(null,user);
} else{
return done(null,false);
}
}); });
express.js :
app.use(session({secret : 'multivision unicorns', resave : false, saveUninitialized : false})); app.use(passport.initialize()); app.use(passport.session());
routes.js :
app.post('/login', function(req,resp,next){
console.log("hitting /login call in routes");
var auth = passport.authenticate('local', function(err,user){
console.log("Calling auth function");
console.log("User data : " + user);
if(err) {return next(err);}
if(!user){
resp.send({success : false});
}
req.logIn(user, function(err){
if(err) { return next(err);}
resp.send({success:true, user:user});
})
});
auth(req,resp,next);
});
in controller :
angular.module('app').controller('navbarCtrl', function($scope,$http){
$scope.signIn = function(username,password){
console.log("I am trying to login to website");
$http.post('/login',{username:username}).then(function(response){
if(response.data.success){
console.log('logged in to webiste buddy!!!');
}
else{
console.log("Unable to login DUDE!!!!");
}
});
}
});
terminal Output :
hitting /login call in routes Calling auth function User data : false (User data is always false even though I tried with the existing user data in DB) POST /login 200 29.125 ms - 17
链接地址: http://www.djcxy.com/p/83212.html