Skip to content

Commit 202509c

Browse files
committed
fix(handling expiry): Serialise the handling of expiry instead of asynchrnous mainly to reduce complexity as well as avoiding issues related to iron-localstorage where events are not being triggered as expected. See PolymerElements/iron-localstorage#19.
1 parent e909f9d commit 202509c

File tree

2 files changed

+44
-11
lines changed

2 files changed

+44
-11
lines changed

auth0-auth.html

+34-7
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
<dom-module id="auth0-auth">
88

99
<template>
10-
<template is="dom-if" if="{{jwtManager}}">
11-
<jwt-manager name="auth0:authUser" on-auth-token="_handleTokenEvent"></jwt-manager>
12-
</template>
10+
<!--<template is="dom-if" if="{{jwtManager}}">-->
11+
<jwt-manager id="jwtManager" name="auth0:authUser" jwt="[[jwt]]" on-auth-token="_handleTokenEvent"></jwt-manager>
12+
<!--</template>-->
1313
<iron-ajax
1414
id="ajax"
1515
method="DELETE">
@@ -103,11 +103,11 @@
103103
_parseHash: function(){
104104
var idToken = localStorage.getItem('auth0:authUser');
105105
var accessToken = localStorage.getItem('auth0:accessToken');
106-
if (idToken && accessToken) {
106+
if (idToken && accessToken && this.$.jwtManager.tokenIsValid(idToken)) {
107107
this.auth0.client.userInfo(accessToken, function(err, user) {
108108
this._setUserProfile(user);
109109
this._setIdToken(idToken);
110-
110+
this.$.jwtManager.monitorExpiry(idToken);
111111
}.bind(this));
112112
return
113113
}
@@ -120,6 +120,7 @@
120120
localStorage.setItem('auth0:authUser', authResult.idToken);
121121
localStorage.setItem('auth0:accessToken', authResult.accessToken);
122122
this._setIdToken(authResult.idToken);
123+
this.jwt = authResult.idToken;
123124
return
124125
} else {
125126
this.auth0.renewAuth({
@@ -137,11 +138,13 @@
137138
var lock = new Auth0Lock(this.clientId, this.domain, this.options);
138139
lock.show();
139140
} else {
141+
console.log('renewAuth........')
140142
this.auth0.client.userInfo(result.accessToken, function(err, user) {
141143
this._setUserProfile(user);
142144
}.bind(this));
143-
localStorage.setItem('auth0:authUser', result.idToken);
145+
// localStorage.setItem('auth0:authUser', result.idToken);
144146
localStorage.setItem('auth0:accessToken', result.accessToken);
147+
this.jwt = result.idToken;
145148
this._setIdToken(result.idToken);
146149
}
147150
}.bind(this));
@@ -151,7 +154,31 @@
151154

152155
_handleTokenEvent: function (event) {
153156
if(event.detail.status === 'EXPIRED') {
154-
this.signOut(this.clientId)
157+
console.log('expired')
158+
this.auth0.renewAuth({
159+
redirectUri: window.location.origin + '?sso-silent-auth=true',
160+
scope: this.options.auth.scope,
161+
usePostMessage: true
162+
}, function (err, result) {
163+
if (err || !result || !result.idToken || !result.accessToken) {
164+
// regular login
165+
if(this.hostedPages) {
166+
this.auth0.authorize(this.options.auth);
167+
return
168+
}
169+
//else
170+
var lock = new Auth0Lock(this.clientId, this.domain, this.options);
171+
lock.show();
172+
} else {
173+
this.auth0.client.userInfo(result.accessToken, function(err, user) {
174+
this._setUserProfile(user);
175+
}.bind(this));
176+
localStorage.setItem('auth0:authUser', result.idToken);
177+
localStorage.setItem('auth0:accessToken', result.accessToken);
178+
this._setIdToken(result.idToken);
179+
this.jwt = result.idToken;
180+
}
181+
}.bind(this));
155182
}
156183
},
157184

jwt-manager.html

+10-4
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
<template>
88
<iron-localstorage use-raw
99
name="[[name]]"
10-
value="{{jwt}}"
11-
on-iron-localstorage-load="_monitorExpiry">
10+
value="[[jwt]]">
1211
</iron-localstorage>
1312
</template>
1413

@@ -30,16 +29,23 @@
3029
},
3130

3231
observers: [
32+
'monitorExpiry(jwt)'
3333
],
3434

35-
_monitorExpiry: function (event) {
36-
var decoded = jwt_decode(this.jwt);
35+
monitorExpiry: function (jwt) {
36+
console.log('_monitorExpiry')
37+
var decoded = jwt_decode(jwt);
3738
var timeToExpiryInMilliseconds= decoded.exp * 1000 - Date.now();
3839

3940
this.async(function () {
4041
this.fire('auth-token', {status: 'EXPIRED'})
4142
}.bind(this), timeToExpiryInMilliseconds)
43+
},
4244

45+
tokenIsValid: function (jwt) {
46+
var decoded = jwt_decode(jwt);
47+
var timeToExpiryInMilliseconds= decoded.exp * 1000 - Date.now();
48+
return timeToExpiryInMilliseconds > 0 ? true : false
4349
}
4450
});
4551

0 commit comments

Comments
 (0)