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");
break;
}
});
}]);
app.provider('loginSecurity', function() {
this.$get = ['$http', '$q', function($http, $q) {
var service = {
defer: $q.defer,
requireAuth: function() {
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();
}}
});
$routeProvider.when('/login', {
templateUrl: 'static/scripts/login/template.html',
controller: 'Login'
});
$routeProvider.otherwise({redirectTo: '/404'});
}]);
:
- . " " .
- $q requireAuth, . , ?
- - , getLoginStatus, , , getLoginStatus , . , .
- # 3, $routeProvider.. , , , # 1.
. , . .