Browse Source

refactor: token generation with token (WIP)

pull/9784/head
Pranav C 2 weeks 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 // Try request again with new token
return axiosInstance return axiosInstance
.post('/auth/token/refresh', null, { .post('/auth/token/refresh', null, {

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

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

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

@ -91,8 +91,8 @@ export interface SignOutParams {
export interface Actions { export interface Actions {
signOut: (signOutParams?: SignOutParams) => Promise<void> signOut: (signOutParams?: SignOutParams) => Promise<void>
signIn: (token: string, keepProps?: boolean) => Promise<void> signIn: (token: string, keepProps?: boolean) => void
refreshToken: () => void refreshToken: () => 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