Browse Source

refactor: use `useSharedExecutionFn` composable for refresh token

pull/9784/head
Pranav C 1 week ago
parent
commit
adf56979ea
  1. 20
      packages/nc-gui/composables/useGlobal/actions.ts
  2. 2
      packages/nc-gui/composables/useGlobal/types.ts

20
packages/nc-gui/composables/useGlobal/actions.ts

@ -1,10 +1,8 @@
import { getActivePinia } from 'pinia' import { getActivePinia } from 'pinia'
import { useStorage } from '@vueuse/core'
import type { Actions, AppInfo, Getters, State } from './types' import type { Actions, AppInfo, Getters, State } from './types'
import type { NcProjectType } from '#imports' import type { NcProjectType } from '#imports'
export function useGlobalActions(state: State, getters: Getters): Actions { export function useGlobalActions(state: State, getters: Getters): Actions {
const isTokenRefreshInProgress = useStorage(TOKEN_REFRESH_PROGRESS_KEY, false)
const isTokenUpdatedTab = useState('isTokenUpdatedTab', () => false) const isTokenUpdatedTab = useState('isTokenUpdatedTab', () => false)
const setIsMobileMode = (isMobileMode: boolean) => { const setIsMobileMode = (isMobileMode: boolean) => {
@ -65,7 +63,7 @@ export function useGlobalActions(state: State, getters: Getters): Actions {
} }
/** manually try to refresh token */ /** manually try to refresh token */
const refreshToken = async ({ const _refreshToken = async ({
axiosInstance, axiosInstance,
skipSignOut = false, skipSignOut = false,
}: { }: {
@ -75,16 +73,6 @@ export function useGlobalActions(state: State, getters: Getters): Actions {
const nuxtApp = useNuxtApp() const nuxtApp = useNuxtApp()
const t = nuxtApp.vueApp.i18n.global.t const t = nuxtApp.vueApp.i18n.global.t
// if token refresh is already in progress, wait until it is completed or timeout
if (isTokenRefreshInProgress.value) {
await until(isTokenRefreshInProgress).toMatch((v) => !v, { timeout: 10000 })
// if token is already refreshed and valid return the token
if (getters.signedIn.value && state.token.value) {
isTokenRefreshInProgress.value = false
return state.token.value
}
}
isTokenRefreshInProgress.value = true isTokenRefreshInProgress.value = true
if (!axiosInstance) { if (!axiosInstance) {
@ -108,11 +96,13 @@ export function useGlobalActions(state: State, getters: Getters): Actions {
message.error(t('msg.error.youHaveBeenSignedOut')) message.error(t('msg.error.youHaveBeenSignedOut'))
} }
return null return null
} finally {
isTokenRefreshInProgress.value = false
} }
} }
const refreshToken = useSharedExecutionFn('refreshToken', _refreshToken, {
timeout: 10000,
})
const loadAppInfo = async () => { const loadAppInfo = async () => {
try { try {
const nuxtApp = useNuxtApp() const nuxtApp = useNuxtApp()

2
packages/nc-gui/composables/useGlobal/types.ts

@ -92,7 +92,7 @@ export interface SignOutParams {
export interface Actions { export interface Actions {
signOut: (signOutParams?: SignOutParams) => Promise<void> signOut: (signOutParams?: SignOutParams) => Promise<void>
signIn: (token: string, keepProps?: boolean) => void signIn: (token: string, keepProps?: boolean) => void
refreshToken: (params: { axiosInstance?: AxiosInstance; skipLogout?: boolean }) => Promise<void> refreshToken: (params: { axiosInstance?: AxiosInstance; skipLogout?: boolean; cognitoOnly?: boolean }) => Promise<void>
loadAppInfo: () => void loadAppInfo: () => void
setIsMobileMode: (isMobileMode: boolean) => void setIsMobileMode: (isMobileMode: boolean) => void
navigateToProject: (params: { workspaceId?: string; baseId?: string; type?: NcProjectType; query?: any }) => void navigateToProject: (params: { workspaceId?: string; baseId?: string; type?: NcProjectType; query?: any }) => void

Loading…
Cancel
Save