jeudi 2 avril 2015

passportjs stuck at res.isAuthenticated()

I am new to Angularjs and also Javascript..


I am implementing passport authentication in my Angularjs project.... registration and login is working.....


But after logging in or routing to any other views or states, I am checking whether the user is authenticated...but the function that triggers to check whether the user is valid is stuck at executing query like the following:



<pre><code>

var checkLoggedin=function($q, $timeout, $http, $location, $rootScope){
debugger;

// Initialize a new promise
var deferred = $q.defer();

// Make an AJAX call to check if the user is logged in
$http.get('/loggedin').success(function(user){
debugger;
// Authenticated
if (user !== '0')
deferred.resolve();

// Not Authenticated
else {
$rootScope.message = 'You need to log in.';
deferred.reject();
$location.url('/login');
}
});
return deferred.promise;
};

</code></pre>


But when it calls $http.get('/loggedin') in the console I would get:



serializing user:26
POST /login 200 413.520 ms - 164
Executing (default): SELECT "usid" AS "id", "usname" AS "username", "uspassword" AS "password", "usemail" AS "email" FROM "dbuser" AS "User" LIMIT 1;

and it will be stuck there and in my ui-state config I have
<pre><code>
.state('dashboard', {
url: "/dashboard",
templateUrl: 'Dashboard/View/dashboard.html',
controller:'DashboardController',
resolve: {
loggedin:checkLoggedin
}
})
</code></pre>


so my state waiting for the checkloggedin to be resolved...but its stuck there nothing response I am getting...Is there any way I verify the user is logged in....I searched but couldn't find a way its very trickier.....hope some one has faced same problem....


I have implemented serialize and deserialize as follows



<pre><code>
module.exports = function(passport){
passport.serializeUser(function(user, done) {
console.log('serializing user:'+user.id);
done(null,user.id);
});

passport.deserializeUser(function(id, done) {
var collection = User;
collection.findOne({'_id' : id },function(err, user) {
console.log('deserializing user:',user);
done(err, user);
});

});

login(passport);
signup(passport);

}
</code></pre>

Aucun commentaire:

Enregistrer un commentaire