Browse Source

refactor: token generation with token (WIP)

pull/9784/head
Pranav C 1 week ago
parent
commit
4d81489530
  1. 5
      packages/nc-gui/composables/useApi/interceptors.ts
  2. 52
      packages/nc-gui/composables/useGlobal/actions.ts
  3. 4
      packages/nc-gui/composables/useGlobal/types.ts

5
packages/nc-gui/composables/useApi/interceptors.ts

@ -137,6 +137,11 @@ export function addAxiosInterceptors(api: Api<any>) {
})
}
try {
const token = await state.refreshToken()
refreshTokenPromiseRes(token)
} catch (e) {}
// Try request again with new token
return axiosInstance
.post('/auth/token/refresh', null, {

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

@ -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 () => {

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

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

Loading…
Cancel
Save