I am working on a simple website based on angular.js + node.js and mongodb using an express template. I hit using $http from an angular controller using the POST method in the api named users.js , where login authentication is done using the passport.authenticate method. I require a local logic login strategy in users.js .
But it does not work. Here's the angular login service code and the node api user code. Can someone tell me how to use passport.js in angular and node?
angular routing through service
app.service('Auth',function($location,$http,$localStorage){ var userLogin ; return{ setLogIN:function(email,password){ $http({ method: 'POST', url: '/users/login',
node user routing
router.post('/login',passport.authenticate('local', { // use passport-local for authentication successRedirect : '/profile', failureRedirect : '/login', failureFlash : true }));
passport-local strategy
app.use(passport.initialize()); app.use(passport.session()); passport.use(new LocalStrategy( function (username, password, done) { User.findOne({username: username}, function (err, user) { if (err) { return done(err); } if (!user) { return done(null, false, {alert: 'Incorrect username.'}); } if (user.password != password) { return done(null, false, {alert: 'Incorrect password.'}); } return done(null, user); }); } )); passport.serializeUser(function(user, done) { done(null, user.id); }); passport.deserializeUser(function(id, done) { User.findById(id, function(err, user) { done(err, user); }); }); function isAuthenticated(req,res,next){ if(req.isAuthenticated())return next(); res.redirect('/'); }
So, I want to authenticate using a passport, but use client-side templating / routing to support proper authentication.
Can anyone point me in the right direction? Or tell me that what I am doing is completely wrong?
edit: the error I get with my code is not redirected to the profile page
TypeError: POST http: // localhost: 3000 / users / login 500 Internal Server Error
Invalid user