0

I'm very stuck in very small and stupid problem :) can't find the way out, why this is not working...

$scope.IsAuthenticated = 0;
function checkAuthState() {
    var req = {
        method: 'GET',
        url: 'api/user/check_auth',
        headers: {
            'X-CSRF-TOKEN': $('input[name="_csrf"]').val(),
            'Content-Type': 'application/json'
        }
    }

    $http(req)
        .then(function(data){
            console.log(data);
            console.log(JSON.stringify(data.data));
            if(JSON.stringify(data.data) === 'true'){
                console.log("here");
                $scope.IsAuthenticated = 1;
                console.log("Set the value: "+$scope.IsAuthenticated);
            }
            else console.log('not equal');
        }).finally(function() {
             console.log("Current is auth state: "+ $scope.IsAuthenticated);
        });

    console.log("Current, before exit from method: "+ $scope.IsAuthenticated);
    return $scope.IsAuthenticated;
}

this is the log output

main.js:53 Current, before exit from method: 0
main.js:21 0
main.js:53 Current, before exit from method: 0
main.js:41 Object {data: true, status: 200, config: Object, statusText: "OK"}
main.js:42 true
main.js:44 here
main.js:46 Set the value: 1
main.js:50 Current is auth state: 1
main.js:41 Object {data: true, status: 200, config: Object, statusText: "OK"}
main.js:42 true
main.js:44 here
main.js:46 Set the value: 1
main.js:50 Current is auth state: 1

$scope.IsAuthenticated has been changed. It's a global controller's scope variable. it's set outside of function.

How come this function never return assigned value(i tried boolean and this, Integer, but seems like it doesn't make a difference)?

user1935987
  • 3,136
  • 9
  • 56
  • 108

0 Answers0