Hi everyone!
I've been trying for too long now, I need your help!
What I'm trying to achieve here is that I need to validate server-side
(PHP) if the user is logged in or not. Then if he's not, I redirect to the
login page.
It's working good until I reach the point where it redirects to the login
page. What I found is that even if I do event.preventDefault(), angular
will still load my dashboard.html partial and then redirect to login.
That's what is bothering right now.. If I want the user to be redirect, I
don't want angular to load my partial view before redirecting..
I'm guessing that it's because of the async call.. But I don't know how to
get around this problem!
Here's my code
sruApp.config(['$routeProvider',
function($routeProvider) {
$routeProvider.
when('/', {
templateUrl: './partials/home.html',
controller: 'HomeCtrl',
authenticate: false
}).
when('/login', {
templateUrl: './partials/login.html',
controller: 'LoginCtrl',
authenticate: false
}).
when('/dashboard', {
templateUrl: './partials/dashboard.html',
controller: 'DashboardCtrl',
authenticate: true
}).
when('/logout', {
templateUrl: './partials/logout.html',
controller: 'LogoutCtrl',
authenticate: false
}).
when('/register', {
templateUrl: './partials/register.html',
controller: 'RegisterCtrl',
authenticate: false
});
}]);
sruApp.run(["$rootScope", "$location", "user", function($rootScope,
$location, user) {
$rootScope.$on('$routeChangeStart', function (event, next, current) {
user.isLoggedIn().then(function(res) { //async call to php api return if
$_SESSION["isLoggedIn"] == true || false
$rootScope.isLoggedIn = res.result;
if(next.$$route.authenticate) {
if(!res.result) {
event.preventDefault();
$location.path("/login");
}
}
});
});
}]);
user factory :
sruApp.factory("user", ['$rootScope', '$http', function($rootScope, $http) {
var appAPI = "./server/app/";
var obj = {};
obj.isLoggedIn = function() {
return $http.get(appAPI + 'user/isLoggedIn');
};
obj.logout = function() {
return $http.get(appAPI + 'user/logout');
};
return obj;
}]);
I hope someone will be able to help me with this! i'm struggling with this
problem for hours now..
Thanks!
--
You received this message because you are subscribed to the Google Groups
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.