I am new to Angular JS and Django. I am working on a project in which back end is implemented using Django framework. In this project multiple API's are there. Our front end is developed using Angular JS (Actually it is Angular JS Single page Application).I want to know that what would be the way to put a check condition that user is logged in or not before each API call.Because we want to reduce unecessary Api calling untill a user logged in. Currently , However we have implemented authentication mechanism in our back end part but we also want to implement some mechanism in front end so that untill a user logged in ,no request will pass to server.
We want that if user is not logged in, then no api should be called(i.e controller should not be loaded). But, as soon as a user logged in, api should be called without reloading the browser. The solution which I have tried I am providing in following section.please correct me because I know that surely I am doing something wrongly. In rsf function , I am confused what should be written inside if statement.
Please suggest some method. Thanks in advance.
myApp.config(function($routeProvider, $locationProvider) {
var rsf = {
// This function tells if user is logged or not
"check": function($localStorage, $location) {
if ($localStorage.user != null) {
alert("You are logged in");
} else {
throw "Not logged in";
}
}
};
$routeProvider.when('/login', {
templateUrl: 'vues/login.html',
controller: 'neutreCtrl'
}).when('/compte', {
templateUrl: 'vues/compte.html',
controller: 'compteCtrl',
resolve: rsf
}).otherwise({
redirectTo: '/login'
});
})