Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use passport with angular node app?

I am working on a simple blog website based on angular.js + node.js and mongodb using express template. I hit with $http from angular controller by POST method to a api named users.js where login is authenticated using passport.authenticate method. I require passport-local login strategies in users.js.
But it's not working.here is angular login service code and node users api code. Can anybody tell me how can use passport.js in angular and node?

angular routing through a 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 routing in user

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 passport, but use the client side templating/routing to keep the proper authentication.

Can someone please point me in the right direction? Or tell me if what I am doing is completely misguided?

edit : the error I AM getting with my code is it's not redirecting to profile page

TypeError: POST http://localhost:3000/users/login 500 Internal Server Error

Not a valid User

like image 418
RohanArihant Avatar asked Apr 22 '16 05:04

RohanArihant


People also ask

What is passport in angular?

Passport is authentication middleware for Node. js. Extremely flexible and modular, Passport can be unobtrusively dropped into any Express-based web application. A comprehensive set of strategies support authentication using a username and password, Facebook, Twitter and more.

How does NodeJS passport work?

Passport is a popular, modular authentication middleware for Node. js applications. With it, authentication can be easily integrated into any Node- and Express-based app. The Passport library provides more than 500 authentication mechanisms, including OAuth, JWT, and simple username and password based authentication.

What is the passport in NodeJS?

Passport is Express-compatible authentication middleware for Node. js. Passport's sole purpose is to authenticate requests, which it does through an extensible set of plugins known as strategies.


1 Answers

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

 //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 POST method and hit to node (users.js) controller and get response from it. if user authentication is successful then it relocate to profile view otherwise remain on login view.

//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 a hit from angular side throught post method it use passport-local method for authentication if user is authenticated seccessfully then authenticated user is sent as response..

like image 52
RohanArihant Avatar answered Oct 06 '22 23:10

RohanArihant