Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent duplicate token refresh calls #2205

Merged
merged 1 commit into from
Oct 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions src/services/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,6 @@ export function initVueAuthenticate (config) {
Log.logger = console
Log.level = openIdConfig.logLevel

mgr.events.addUserLoaded(function (user) {
console.log('New User Loaded:', arguments)
console.log('Access_token: ', user.access_token)
})

mgr.events.addSilentRenewError(function () {
console.error('Silent Renew Error:', arguments)
})

mgr.events.addUserSignedOut(function () {
console.log('UserSignedOut:', arguments)
})
Expand Down
44 changes: 24 additions & 20 deletions src/store/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ const state = {

const actions = {
cleanUpLoginState (context) {
if (context.state.id === '') {
return
}
// reset user to default state
context.commit('SET_USER', state)
// reset capabilities to default state
Expand All @@ -37,10 +40,6 @@ const actions = {
})
},
initAuth (context, payload = { autoRedirect: false }) {
// if called from login, use available vue-authenticate instance; else re-init
if (!vueAuthInstance) vueAuthInstance = initVueAuthenticate(context.rootState.config)
const token = vueAuthInstance.getToken()

function init (client, token) {
const instance = context.rootState.config.server || window.location.origin
const options = {
Expand Down Expand Up @@ -87,28 +86,33 @@ const actions = {
})
}

const tokenRefresh = function (client) {
vueAuthInstance.mgr.signinSilent().then(user => {
console.log('token refreshed…')
init(client, user.access_token)
}).catch(error => {
console.warn('token refresh failed ' + error)
context.dispatch('cleanUpLoginState')
router.push({ name: 'accessDenied' })
})
}

if (token) {
// if called from login, use available vue-authenticate instance; else re-init
if (!vueAuthInstance) {
vueAuthInstance = initVueAuthenticate(context.rootState.config)
const client = this._vm.$client
vueAuthInstance.events().addAccessTokenExpired(function () {
console.log('store/user.js - AccessToken Expired:', arguments)
tokenRefresh(client)
console.log('AccessToken Expired:', arguments)
})
vueAuthInstance.mgr.events.addAccessTokenExpiring(function () {
console.log('AccessToken Expiring:', arguments)
tokenRefresh(client)
})

vueAuthInstance.events().addUserLoaded(user => {
console.log(`New User Loaded. access_token: ${user.access_token}, refresh_token: ${user.refresh_token}`)
init(client, user.access_token)
})
vueAuthInstance.events().addUserUnloaded(() => {
console.log('user unloaded…')
context.dispatch('cleanUpLoginState')
router.push({ name: 'accessDenied' })
})
vueAuthInstance.events().addSilentRenewError(error => {
console.error('Silent Renew Error:', error)
context.dispatch('cleanUpLoginState')
router.push({ name: 'accessDenied' })
})
}
const token = vueAuthInstance.getToken()
if (token) {
init(this._vm.$client, token)
}
},
Expand Down