Skip to content

Commit

Permalink
fix(handling expiry): Serialise the handling of expiry instead of asy…
Browse files Browse the repository at this point in the history
…nchrnous 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.
  • Loading branch information
johnlim committed Jul 11, 2017
1 parent e909f9d commit 202509c
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 11 deletions.
41 changes: 34 additions & 7 deletions auth0-auth.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
<dom-module id="auth0-auth">

<template>
<template is="dom-if" if="{{jwtManager}}">
<jwt-manager name="auth0:authUser" on-auth-token="_handleTokenEvent"></jwt-manager>
</template>
<!--<template is="dom-if" if="{{jwtManager}}">-->
<jwt-manager id="jwtManager" name="auth0:authUser" jwt="[[jwt]]" on-auth-token="_handleTokenEvent"></jwt-manager>
<!--</template>-->
<iron-ajax
id="ajax"
method="DELETE">
Expand Down Expand Up @@ -103,11 +103,11 @@
_parseHash: function(){
var idToken = localStorage.getItem('auth0:authUser');
var accessToken = localStorage.getItem('auth0:accessToken');
if (idToken && accessToken) {
if (idToken && accessToken && this.$.jwtManager.tokenIsValid(idToken)) {
this.auth0.client.userInfo(accessToken, function(err, user) {
this._setUserProfile(user);
this._setIdToken(idToken);

this.$.jwtManager.monitorExpiry(idToken);
}.bind(this));
return
}
Expand All @@ -120,6 +120,7 @@
localStorage.setItem('auth0:authUser', authResult.idToken);
localStorage.setItem('auth0:accessToken', authResult.accessToken);
this._setIdToken(authResult.idToken);
this.jwt = authResult.idToken;
return
} else {
this.auth0.renewAuth({
Expand All @@ -137,11 +138,13 @@
var lock = new Auth0Lock(this.clientId, this.domain, this.options);
lock.show();
} else {
console.log('renewAuth........')
this.auth0.client.userInfo(result.accessToken, function(err, user) {
this._setUserProfile(user);
}.bind(this));
localStorage.setItem('auth0:authUser', result.idToken);
// localStorage.setItem('auth0:authUser', result.idToken);
localStorage.setItem('auth0:accessToken', result.accessToken);
this.jwt = result.idToken;
this._setIdToken(result.idToken);
}
}.bind(this));
Expand All @@ -151,7 +154,31 @@

_handleTokenEvent: function (event) {
if(event.detail.status === 'EXPIRED') {
this.signOut(this.clientId)
console.log('expired')
this.auth0.renewAuth({
redirectUri: window.location.origin + '?sso-silent-auth=true',
scope: this.options.auth.scope,
usePostMessage: true
}, function (err, result) {
if (err || !result || !result.idToken || !result.accessToken) {
// regular login
if(this.hostedPages) {
this.auth0.authorize(this.options.auth);
return
}
//else
var lock = new Auth0Lock(this.clientId, this.domain, this.options);
lock.show();
} else {
this.auth0.client.userInfo(result.accessToken, function(err, user) {
this._setUserProfile(user);
}.bind(this));
localStorage.setItem('auth0:authUser', result.idToken);
localStorage.setItem('auth0:accessToken', result.accessToken);
this._setIdToken(result.idToken);
this.jwt = result.idToken;
}
}.bind(this));
}
},

Expand Down
14 changes: 10 additions & 4 deletions jwt-manager.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
<template>
<iron-localstorage use-raw
name="[[name]]"
value="{{jwt}}"
on-iron-localstorage-load="_monitorExpiry">
value="[[jwt]]">
</iron-localstorage>
</template>

Expand All @@ -30,16 +29,23 @@
},

observers: [
'monitorExpiry(jwt)'
],

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

this.async(function () {
this.fire('auth-token', {status: 'EXPIRED'})
}.bind(this), timeToExpiryInMilliseconds)
},

tokenIsValid: function (jwt) {
var decoded = jwt_decode(jwt);
var timeToExpiryInMilliseconds= decoded.exp * 1000 - Date.now();
return timeToExpiryInMilliseconds > 0 ? true : false
}
});

Expand Down

0 comments on commit 202509c

Please sign in to comment.