Skip to content
This repository was archived by the owner on Jan 22, 2019. It is now read-only.

Commit 909d86c

Browse files
ciaranjciaranj
authored andcommitted
Check for expired tokens on route change.
* The absence of a token should be considered the same as having a token but it being expired. In both scenarios now on a route change the user will be redirected to the IDP
1 parent 963ecee commit 909d86c

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

dist/angularJsOAuth2.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
//var state = Date.now() + "" + Math.random();
1010

1111
(function() {
12+
function expired(token) {
13+
return (token && token.expires_at && new Date(token.expires_at) < new Date());
14+
};
1215
function getSessionToken($window) {
1316
var tokenString = $window.sessionStorage.getItem('token');
1417
var token = null;
@@ -133,9 +136,6 @@
133136

134137
// Auth interceptor - if token is missing or has expired this broadcasts an authRequired event
135138
angular.module('oauth2.interceptor', []).factory('OAuth2Interceptor', ['$rootScope', '$q', '$window', function ($rootScope, $q, $window) {
136-
var expired = function(token) {
137-
return (token && token.expires_at && new Date(token.expires_at) < new Date());
138-
};
139139

140140
var service = {
141141
request: function(config) {
@@ -185,6 +185,7 @@
185185
angular.module('oauth2.endpoint', []).factory('Endpoint', ['AccessToken', '$window', function(accessToken, $window) {
186186
var service = {
187187
authorize: function() {
188+
accessToken.destroy();
188189
$window.sessionStorage.setItem('verifyState', service.state);
189190
window.location.replace(service.url);
190191
},
@@ -266,7 +267,7 @@
266267

267268
function routeChangeHandler(event, nextRoute) {
268269
if (nextRoute.$$route && nextRoute.$$route.requireToken) {
269-
if (!accessToken.get()) {
270+
if (!accessToken.get() || expired(accessToken.get())) {
270271
event.preventDefault();
271272
$window.sessionStorage.setItem('oauthRedirectRoute', $location.path());
272273
endpoint.authorize();
@@ -309,6 +310,7 @@
309310
});
310311
scope.$on('oauth2:authExpired', function() {
311312
scope.signedIn = false;
313+
accessToken.destroy();
312314
});
313315
$rootScope.$on('$routeChangeStart', routeChangeHandler);
314316
}

0 commit comments

Comments
 (0)