I'm trying to make a function that only shows a certain part of the html to users that have the right role. These are my functions:
nodejs/server.js
app.get('/api/isadmin', ensureAuthenticated, function (req, res) {
User.findById(req.user, function (err, user) {
res.send(user);
});
});
This is where the problem occurs, it will run indefintely:
<ul ng-if="isAdmin()">
<li>
<a ui-sref="admin">Admin</a>
</li>
</ul>
Why is this happening? I've looked at Satellizer's isAuthenticated function and basically done the same thing.
EDIT: Updated code:
adminservice.js
angular.module('App')
.factory('AdminService', function ($q, $http, toastr) {
return {
getAdmin: function () {
var httpget = $http.get('http://localhost:3000/api/isadmin');
httpget.then(function (response) {
var res = response.data.role;
if (res == 'admin') {
console.log('true');
return true;
}
else {
console.log('false');
return false;
}
}, function (result) {
return $q.reject(result);
});
return httpget;
}
};
});
navbar.js
angular.module('App')
.controller('NavbarCtrl', function($q, $scope, $auth, AdminService) {
$scope.isAuthenticated = function() {
return $auth.isAuthenticated();
};
AdminService.getAdmin().then(
function (result) {
$scope.isAdmin = result;
});
});
both responses, true or false, can still see the Admin list element.