Skip to content

Commit

Permalink
Add invoke for redirector
Browse files Browse the repository at this point in the history
  • Loading branch information
merqlove committed Oct 18, 2016
1 parent 844b430 commit 466a1f0
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions src/angularJwt/services/authManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,15 @@ angular.module('angular-jwt.authManager', [])
token = tokenGetter();
}
return token;
}
}

function invokeRedirector(redirector) {
if (Array.isArray(redirector)) {
return $injector.invoke(redirector, this, {});
} else {
return redirector($location);
}
}

$rootScope.isAuthenticated = false;

Expand All @@ -40,16 +48,11 @@ angular.module('angular-jwt.authManager', [])

function redirectWhenUnauthenticated() {
$rootScope.$on('unauthenticated', function () {
var redirector = config.unauthenticatedRedirector;
if (Array.isArray(redirector)) {
$injector.invoke(redirector, this, {});
} else {
config.unauthenticatedRedirector($location);
}
invokeRedirector(config.unauthenticatedRedirector);
unauthenticate();
});
}

function verifyRoute(event, next) {
if (!next) {
return false;
Expand All @@ -60,7 +63,7 @@ angular.module('angular-jwt.authManager', [])
if (routeData && routeData.requiresLogin === true) {
var token = invokeToken(config.tokenGetter);
if (!token || jwtHelper.isTokenExpired(token)) {
config.unauthenticatedRedirector($location);
invokeRedirector(config.unauthenticatedRedirector);
event.preventDefault();
}
}
Expand All @@ -72,8 +75,10 @@ angular.module('angular-jwt.authManager', [])
return {
authenticate: authenticate,
unauthenticate: unauthenticate,
getToken: function(){ return invokeToken(config.tokenGetter); },
redirect: function() { return invokeRedirector(config.unauthenticatedRedirector); },
checkAuthOnRefresh: checkAuthOnRefresh,
redirectWhenUnauthenticated: redirectWhenUnauthenticated
}
}]
});
});

0 comments on commit 466a1f0

Please sign in to comment.