From 4db6d98011ce0952822f78f75461009275b9daa2 Mon Sep 17 00:00:00 2001 From: "Daniel J. Summers" Date: Sun, 8 Oct 2017 21:58:36 -0500 Subject: [PATCH] Implemented auth renewal (#5) --- src/app/src/auth/AuthService.js | 34 ++++++++++++++++++++++++++++++++- src/app/src/store/index.js | 1 + src/app/static/silent.html | 22 +++++++++++++++++++++ 3 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 src/app/static/silent.html diff --git a/src/app/src/auth/AuthService.js b/src/app/src/auth/AuthService.js index 9562568..51544f9 100644 --- a/src/app/src/auth/AuthService.js +++ b/src/app/src/auth/AuthService.js @@ -5,6 +5,8 @@ import auth0 from 'auth0-js' import AUTH_CONFIG from './auth0-variables' import mutations from '@/store/mutation-types' +var tokenRenewalTimeout + export default class AuthService { constructor () { @@ -17,7 +19,7 @@ export default class AuthService { auth0 = new auth0.WebAuth({ domain: AUTH_CONFIG.domain, clientID: AUTH_CONFIG.clientId, - redirectUri: AUTH_CONFIG.callbackUrl, + redirectUri: AUTH_CONFIG.appDomain + AUTH_CONFIG.callbackUrl, audience: `https://${AUTH_CONFIG.domain}/userinfo`, responseType: 'token id_token', scope: 'openid profile email' @@ -78,6 +80,16 @@ export default class AuthService { }) } + scheduleRenewal () { + let expiresAt = JSON.parse(localStorage.getItem('expires_at')) + let delay = expiresAt - Date.now() + if (delay > 0) { + tokenRenewalTimeout = setTimeout(() => { + this.renewToken() + }, delay) + } + } + setSession (authResult) { // Set the time that the access token will expire at let expiresAt = JSON.stringify( @@ -86,10 +98,30 @@ export default class AuthService { localStorage.setItem('access_token', authResult.accessToken) localStorage.setItem('id_token', authResult.idToken) localStorage.setItem('expires_at', expiresAt) + this.scheduleRenewal() + } + + renewToken () { + console.log('attempting renewal...') + this.auth0.renewAuth( + { + audience: `https://${AUTH_CONFIG.domain}/userinfo`, + redirectUri: `${AUTH_CONFIG.appDomain}/static/silent.html`, + usePostMessage: true + }, + (err, result) => { + if (err) { + console.log(err) + } else { + this.setSession(result) + } + } + ) } logout (store, router) { // Clear access token and ID token from local storage + clearTimeout(tokenRenewalTimeout) localStorage.removeItem('access_token') localStorage.removeItem('id_token') localStorage.removeItem('expires_at') diff --git a/src/app/src/store/index.js b/src/app/src/store/index.js index d98248b..739a2ec 100644 --- a/src/app/src/store/index.js +++ b/src/app/src/store/index.js @@ -34,6 +34,7 @@ export default new Vuex.Store({ state: { user: JSON.parse(localStorage.getItem('user_profile') || '{}'), isAuthenticated: (() => { + this.auth0.scheduleRenewal() if (this.auth0.isAuthenticated()) { api.setBearer(localStorage.getItem('id_token')) } diff --git a/src/app/static/silent.html b/src/app/static/silent.html new file mode 100644 index 0000000..1a6cae5 --- /dev/null +++ b/src/app/static/silent.html @@ -0,0 +1,22 @@ + + + + + + + + + + \ No newline at end of file