In Angular, how can I get the source data from the server before running?

A little information. I am working on a one-page application, but I am trying to make it only an HTML file, not a real dynamic page containing all the information about it. I also hope that when the application loads (or, possibly, before), check to see if the current session has logged in, and if not, direct the hash to "login".

I am new to Angular and it is difficult for me to determine how to program this thread. So essentially ..

  • HTML page loaded by lazy loading
  • Hit URL to get login status
  • If the status is "not registered", directly to # / login
  • Launch the application

Any pointers that # 2 and # 3 will live on? In my "easy world" I just used jquery to capture this data and then called angular.resumeBootstrap([appname]). But since I am trying to learn Angular, and not just hack into those parts that I do not understand, I would like to know what will be used in this place. I looked at suppliers, but I'm not sure what I need.

Thanks!

EDIT

Based on @ Mik378's answer, I updated my code to the next as a test. It works to a certain extent, but since "get" is asynchronous, it allows the application to continue to load everything that was before taking state results.

var app = angular.module('ping', [
    'ngRoute',
    'ping.controllers'
]).provider('security', function() {
    this.$get = ['$http', function($http) {
            var service = {
                getLoginStatus: function () {
                    if (service.isAuthenticated())
                        return $q.when(service.currentUser);
                    else
                        return $http.get('/login/status').then(function (response) {
                            console.log(response);
                            service.loggedIn = response.data.loggedIn;
                            console.log(service);
                            return service.currentUser;
                        });
                },
                isAuthenticated: function () {
                    return !!service.loggedIn;
                }
            };
            return service;
        }];
}).run(['security', function(security) {
        return security.getLoginStatus().then(function () {
            if(!security.isAuthenticated()) {
                console.log("BADNESS");
            } else {
                console.log("GOODNESS");
            }
        });
}]);

, - , , ( ) , .

EDIT # 2

"" , @Mik378 , . , ( ) , , ( )

angular.module('ping.controllers', [])
        .controller('Dashboard', ['$scope', function($scope) {
            console.log('dashboard')
        }])
        .controller('Login', ['$scope', function($scope) {
            console.log('login')
        }]);

var app = angular.module('ping', [
    'ngRoute',
    'ping.controllers'
]).run(['$rootScope', '$location', function($root, $location) {
    $root.$on("$routeChangeError", function (event, current, previous, rejection) {
        switch(rejection) {
            case "not logged in":
                $location.path("/login"); //<-- NOTE #1
                break;
        }
    });
}]);

app.provider('loginSecurity', function() {
    this.$get = ['$http', '$q', function($http, $q) {
            var service = {
                defer: $q.defer, //<-- NOTE #2
                requireAuth: function() { //<-- NOTE #3
                    var deferred = service.defer();
                    service.getLoginStatus().then(function() {
                        if (!service.isAuthenticated()) {
                            deferred.reject("not logged in")
                        } else {
                            deferred.resolve("Auth OK")
                        }
                    });
                    return deferred.promise;
                },
                getLoginStatus: function() {
                    if (service.isAuthenticated()) {
                        return $q.when(service.currentUser);
                    } else {
                        return $http.get('/login/status').then(function(response) {
                            console.log(response);
                            service.loggedIn = response.data.loggedIn;
                            console.log(service);
                            return service.currentUser;
                        });
                    }
                },
                isAuthenticated: function() {
                    return !!service.loggedIn;
                }
            };
            return service;
        }
    ];
});

app.config(['$routeProvider', function($routeProvider) {
        console.log('Routing loading');
        $routeProvider.when('/', {
            templateUrl: 'static/scripts/dashboard/template.html',
            controller: 'Dashboard',
            resolve: {'loginSecurity': function (loginSecurity) {
                return loginSecurity.requireAuth(); //<- NOTE #4
            }}
        });
        $routeProvider.when('/login', {
            templateUrl: 'static/scripts/login/template.html',
            controller: 'Login'
        });
        $routeProvider.otherwise({redirectTo: '/404'});
}]);

:

  • . " " .
  • $q requireAuth, . , ?
  • - ​​ , getLoginStatus, , , getLoginStatus , . , .
  • # 3, $routeProvider.. , , , # 1.

. , . .

+4
1

:

angular.module('app').run(['security', '$location', function(security) {
  // Get the current user when the application starts
  // (in case they are still logged in from a previous session)
  security.requestCurrentUser().then(function(){
       if(!security.isAuthenticated())
       $location.path('yourPathToLoginPage')
     };   //service returning the current user, if already logged in
}]);

requestCurrentUser :

requestCurrentUser: function () {
                    if (service.isAuthenticated())
                        return $q.when(service.currentUser);
                    else
                        return $http.get('/api/current-user').then(function (response) {
                            service.currentUser = response.data.user;
                            return service.currentUser;
                        });
                }

security :

isAuthenticated: function () {
                    return !!service.currentUser;
                }

run = > , .

- -

- , , requestCurrentUser, , , resolve.

+2

All Articles