passport.js - PassportJS, AngularJS, $rootScope function is not defined -


i’m building app 1 admin able log in , edit content. when logged in app serve private html doc structure instead of not logged in being presented normal public docs. i’m using angular-route, session , passport authentication. want check if user logged in whenever link page on site. so, if go somewhere they’re not authorized be, can’t edit content on site.

i’m getting $injector:modulerr when run app on localhost. referenceerror says checkauth not defined.

i tried creating global variable using $rootscope suggested here:

app.run(['$rootscope', '$http', function($rootscope, $http){     $rootscope.checkauth = function(){         console.log("rootscope things happening");         $http.get('/login/loggedin').success(function(user){             if (user !== '0') {                 console.log('authenticated');                 return true;             } else {                 console.log('not authenticated');                 return false;             }         });     }; }]); 

so, if user’s logged in, should true , if they’re not logged in, i’ll false. but, still checkauth not defined injector error , never see of console logs.

the angular documentation $rootscope says use format:

$rootscope.scope([providers], [instancecache]); 

so, tried this:

$rootscope.scope(['$rootscope', '$http', function($rootscope, $http){... 

unfortunately, doesn’t work either.

this example of using passport , angular found helpful, they’re allowing multiple users register (which don’t want) , still doesn’t solve question of global function checking if user logged in across multiple pages in website. may see similar controller , route file though.

i know can use factories , services similar suggested in previous similar questions, hoping go route before trying different path. though, if that's best way, i'll give go.

the provided rootscope declaration seems ok me.

assign rootscope value.

app.run(function($rootscope) {     $rootscope.checkauth = function(){         $http.get('/login/loggedin').success(function(user){             if (user !== '0') {                 return true;             } else {                 return false;             }         })     }; }); 

use (don't forget $rootscope dependancy)

app.controller('myctrl', function($scope, $rootscope) {     $scope.authenticated = function() {         return $rootscope.checkauth ;     }; }); 

see live sample compare: http://jsfiddle.net/tmpk5/6/


Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -