|
|
@ -1,8 +1,10 @@ |
|
|
|
import { getActivePinia } from 'pinia' |
|
|
|
import { getActivePinia } from 'pinia' |
|
|
|
|
|
|
|
import { useStorage } from '@vueuse/core' |
|
|
|
import type { Actions, AppInfo, State } from './types' |
|
|
|
import type { Actions, AppInfo, State } from './types' |
|
|
|
import type { NcProjectType } from '#imports' |
|
|
|
import type { NcProjectType } from '#imports' |
|
|
|
|
|
|
|
|
|
|
|
export function useGlobalActions(state: State): Actions { |
|
|
|
export function useGlobalActions(state: State): 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) => { |
|
|
@ -45,7 +47,7 @@ export function useGlobalActions(state: State): Actions { |
|
|
|
/** Sign in by setting the token in localStorage |
|
|
|
/** Sign in by setting the token in localStorage |
|
|
|
* keepProps - is for keeping any existing role info if user id is same as previous user |
|
|
|
* keepProps - is for keeping any existing role info if user id is same as previous user |
|
|
|
* */ |
|
|
|
* */ |
|
|
|
const signIn: Actions['signIn'] = async (newToken, keepProps = false) => { |
|
|
|
const signIn: Actions['signIn'] = (newToken, keepProps = false) => { |
|
|
|
isTokenUpdatedTab.value = true |
|
|
|
isTokenUpdatedTab.value = true |
|
|
|
state.token.value = newToken |
|
|
|
state.token.value = newToken |
|
|
|
|
|
|
|
|
|
|
@ -63,30 +65,36 @@ export function useGlobalActions(state: State): Actions { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** manually try to refresh token */ |
|
|
|
/** manually try to refresh token */ |
|
|
|
const refreshToken = async () => { |
|
|
|
const refreshToken = async ({ |
|
|
|
|
|
|
|
axiosInstance = nuxtApp.$api.instance, |
|
|
|
|
|
|
|
skipSignOut = false, |
|
|
|
|
|
|
|
}: { |
|
|
|
|
|
|
|
axiosInstance?: any |
|
|
|
|
|
|
|
skipSignOut?: boolean |
|
|
|
|
|
|
|
} = {}) => { |
|
|
|
const nuxtApp = useNuxtApp() |
|
|
|
const nuxtApp = useNuxtApp() |
|
|
|
const t = nuxtApp.vueApp.i18n.global.t |
|
|
|
const t = nuxtApp.vueApp.i18n.global.t |
|
|
|
|
|
|
|
isTokenRefreshInProgress.value = true |
|
|
|
return new Promise((resolve) => { |
|
|
|
try { |
|
|
|
nuxtApp.$api.instance |
|
|
|
const response = await axiosInstance.post('/auth/token/refresh', null, { |
|
|
|
.post('/auth/token/refresh', null, { |
|
|
|
withCredentials: true, |
|
|
|
withCredentials: true, |
|
|
|
}) |
|
|
|
}) |
|
|
|
if (response.data?.token) { |
|
|
|
.then((response) => { |
|
|
|
signIn(response.data.token, true) |
|
|
|
if (response.data?.token) { |
|
|
|
return response.data.token |
|
|
|
signIn(response.data.token, true) |
|
|
|
} |
|
|
|
} |
|
|
|
return null |
|
|
|
}) |
|
|
|
} catch (e) { |
|
|
|
.catch(async () => { |
|
|
|
if (state.token.value && state.user.value && !skipSignOut) { |
|
|
|
if (state.token.value && state.user.value) { |
|
|
|
await signOut({ |
|
|
|
await signOut({ |
|
|
|
skipApiCall: true, |
|
|
|
skipApiCall: true, |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
message.error(t('msg.error.youHaveBeenSignedOut')) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}) |
|
|
|
}) |
|
|
|
.finally(() => resolve(true)) |
|
|
|
message.error(t('msg.error.youHaveBeenSignedOut')) |
|
|
|
}) |
|
|
|
} |
|
|
|
|
|
|
|
return null |
|
|
|
|
|
|
|
} finally { |
|
|
|
|
|
|
|
isTokenRefreshInProgress.value = false |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const loadAppInfo = async () => { |
|
|
|
const loadAppInfo = async () => { |
|
|
|