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

Commit 6eae418

Browse files
authored
Merge pull request #4073 from gibkigonzo/bugfix/update-current-token-with-refresh-token
fix current token invalidation with refresh token
2 parents ed5d8a8 + cc8d33a commit 6eae418

File tree

4 files changed

+40
-10
lines changed

4 files changed

+40
-10
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1717
- Fix problem with storeView as dependency in filters - @gibkigonzo (#3968)
1818
- Fix v-model not working in BaseRadioButton - @lukeromanowicz (#4035)
1919
- add disconnect and sync options for clear/cart - @gibkigonzo (#4062)
20+
- Fix current token invalidation with refresh token - @gibkigonzo (#3928, #3620, #3626)
2021

2122
## [1.10.5] - 28.11.2019
2223

core/lib/sync/helpers/index.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
export const hasResponseError = (jsonResponse): boolean => {
2+
if (typeof jsonResponse.result === 'string') {
3+
return true
4+
}
5+
6+
const hasMessage = jsonResponse.result.result || jsonResponse.result.message
7+
8+
return Boolean(hasMessage) && jsonResponse.result.code !== 'ENOTFOUND'
9+
}
10+
11+
export const getResponseMessage = (jsonResponse): string => {
12+
if (typeof jsonResponse.result === 'string') {
13+
return jsonResponse.result
14+
}
15+
16+
if (typeof jsonResponse.result.result === 'string') {
17+
return jsonResponse.result.result
18+
}
19+
20+
return jsonResponse.result.message
21+
}
22+
23+
export const getResponseCode = (jsonResponse): number => {
24+
let responseCode = null
25+
if (jsonResponse.result && jsonResponse.result.code) {
26+
responseCode = parseInt(jsonResponse.result.code)
27+
} else {
28+
responseCode = parseInt(jsonResponse.code)
29+
}
30+
return responseCode
31+
}

core/lib/sync/task.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import Vue from 'vue'
22
import i18n from '@vue-storefront/i18n'
33
import isNaN from 'lodash-es/isNaN'
44
import isUndefined from 'lodash-es/isUndefined'
5-
import toString from 'lodash-es/toString'
65
import fetch from 'isomorphic-fetch'
76
import * as localForage from 'localforage'
87
import rootStore from '@vue-storefront/core/store'
@@ -16,6 +15,7 @@ import { processURLAddress } from '@vue-storefront/core/helpers'
1615
import { serial } from '@vue-storefront/core/helpers'
1716
import config from 'config'
1817
import { onlineHelper } from '@vue-storefront/core/helpers'
18+
import { hasResponseError, getResponseMessage, getResponseCode } from '@vue-storefront/core/lib/sync/helpers'
1919

2020
const AUTO_REFRESH_MAX_ATTEMPTS = 20
2121

@@ -33,7 +33,7 @@ function _sleep (time) {
3333
}
3434

3535
function _internalExecute (resolve, reject, task: Task, currentToken, currentCartId) {
36-
if (currentToken !== null && rootStore.state.userTokenInvalidateLock > 0) { // invalidate lock set
36+
if (currentToken && rootStore.state.userTokenInvalidateLock > 0) { // invalidate lock set
3737
Logger.log('Waiting for rootStore.state.userTokenInvalidateLock to release for ' + task.url, 'sync')()
3838
_sleep(1000).then(() => {
3939
Logger.log('Another try for rootStore.state.userTokenInvalidateLock for ' + task.url, 'sync')()
@@ -42,7 +42,7 @@ function _internalExecute (resolve, reject, task: Task, currentToken, currentCar
4242
return // return but not resolve
4343
} else if (rootStore.state.userTokenInvalidateLock < 0) {
4444
Logger.error('Aborting the network task' + task.url + rootStore.state.userTokenInvalidateLock, 'sync')()
45-
resolve({ code: 401, message: i18n.t('Error refreshing user token. User is not authorized to access the resource') })()
45+
resolve({ code: 401, result: i18n.t('Error refreshing user token. User is not authorized to access the resource') })()
4646
return
4747
} else {
4848
if (rootStore.state.userTokenInvalidated) {
@@ -73,9 +73,9 @@ function _internalExecute (resolve, reject, task: Task, currentToken, currentCar
7373
}
7474
}).then((jsonResponse) => {
7575
if (jsonResponse) {
76-
const responseCode = parseInt(jsonResponse.code)
76+
const responseCode = getResponseCode(jsonResponse)
7777
if (responseCode !== 200) {
78-
if (responseCode === 401 /** unauthorized */ && currentToken !== null) { // the token is no longer valid, try to invalidate it
78+
if (responseCode === 401 /** unauthorized */ && currentToken) { // the token is no longer valid, try to invalidate it
7979
Logger.error('Invalid token - need to be revalidated' + currentToken + task.url + rootStore.state.userTokenInvalidateLock, 'sync')()
8080
if (isNaN(rootStore.state.userTokenInvalidateAttemptsCount) || isUndefined(rootStore.state.userTokenInvalidateAttemptsCount)) rootStore.state.userTokenInvalidateAttemptsCount = 0
8181
if (isNaN(rootStore.state.userTokenInvalidateLock) || isUndefined(rootStore.state.userTokenInvalidateLock)) rootStore.state.userTokenInvalidateLock = 0
@@ -128,12 +128,10 @@ function _internalExecute (resolve, reject, task: Task, currentToken, currentCar
128128
}
129129
}
130130

131-
if (!task.silent && jsonResponse.result && (typeof jsonResponse.result === 'string' || (((jsonResponse.result.result || jsonResponse.result.message) && jsonResponse.result.code !== 'ENOTFOUND') && !silentMode))) {
132-
const message = typeof jsonResponse.result === 'string' ? jsonResponse.result : typeof jsonResponse.result.result === 'string' ? jsonResponse.result.result : jsonResponse.result.message
133-
131+
if (!task.silent && jsonResponse.result && hasResponseError(jsonResponse) && !silentMode) {
134132
rootStore.dispatch('notification/spawnNotification', {
135133
type: 'error',
136-
message: i18n.t(message),
134+
message: i18n.t(getResponseMessage(jsonResponse)),
137135
action1: { label: i18n.t('OK') }
138136
})
139137
}

core/modules/user/hooks/afterRegistration.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as types from './../store/mutation-types'
33

44
export async function afterRegistration ({ Vue, config, store, isServer }) {
55
if (!isServer) {
6-
await store.dispatch('user/startSession')
6+
store.dispatch('user/startSession')
77

88
Vue.prototype.$bus.$on('user-before-logout', () => {
99
store.dispatch('user/logout', { silent: false })

0 commit comments

Comments
 (0)