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