How to use passport with angular node application?

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', //users.js having node routing. data: {email:email, password:password}, }) 

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

+6
source share
2 answers

By default, LocalStrategy expects dictionary parameters to be called username and password .

If you want to use email instead of username , then you should define them in your strategy:

 passport.use(new LocalStrategy({ usernameField: 'email', passwordField: 'password' }, function(username, password, done) { // ... } )); 

In your case, it should be:

 passport.use(new LocalStrategy({ usernameField: 'email', passwordField: 'password' }, 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); }); } )); 
+2
source

I found a solution to my question .. how to use a passport with routing angular -nodejs .......

  //angular js routing $scope.userlogin=function(){ $http({ method:"post", url:'/users/login', data:{username:$scope.username,password:$scope.password}, }).success(function(response){ $scope.userData = response; $localStorage.userData = $scope.userData; console.log("success!!"); $location.path("/profile") }).error(function(response){ console.log("error!!"); $location.path("/login") }); } 

I use the POST method and get into the node controller (users.js) and get a response from it. if user authentication is successful, then it moves to the profile, otherwise it remains in view mode.

 //add these two lines to app.js // var app = express(); app.use(passport.initialize()); app.use(passport.session()); //node routing // add passport-stretegies to users.js 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, { message: 'Incorrect username.' }); } if (user.password != password) { return done(null, false, { message: 'Incorrect password.' }); } return done(null, user); // console.log(user) }); })); //passport serialize user for their session passport.serializeUser(function(user, done) { done(null, user.id); }); //passport deserialize user passport.deserializeUser(function(id, done) { user.findById(id, function(err, user) { done(err, user); }); }); //router on same page router.post('/login',passport.authenticate('local'),function(req,res){ res.send(req.user); //console.log(req.user); }); 

get hit by the angular side throught post method, it uses the passport-local method for authentication if the user is authenticated and then the authenticated user is sent as a response ..

+2
source

All Articles