Browse Source

fix(gui): when navigate to an auth required page try to populate token using refresh token if user is not logged in

Signed-off-by: Pranav C <pranavxc@gmail.com>
pull/5066/head
Pranav C 2 years ago
parent
commit
eb98bb12ad
  1. 1
      packages/nc-gui/components.d.ts
  2. 8
      packages/nc-gui/components/template/Editor.vue
  3. 1
      packages/nc-gui/composables/useApi/interceptors.ts
  4. 3
      packages/nc-gui/composables/useGlobal/actions.ts
  5. 2
      packages/nc-gui/composables/useGlobal/index.ts
  6. 6
      packages/nc-gui/middleware/auth.global.ts

1
packages/nc-gui/components.d.ts vendored

@ -81,7 +81,6 @@ declare module '@vue/runtime-core' {
ClaritySuccessLine: typeof import('~icons/clarity/success-line')['default'] ClaritySuccessLine: typeof import('~icons/clarity/success-line')['default']
EvaEmailOutline: typeof import('~icons/eva/email-outline')['default'] EvaEmailOutline: typeof import('~icons/eva/email-outline')['default']
IcBaselineMoreVert: typeof import('~icons/ic/baseline-more-vert')['default'] IcBaselineMoreVert: typeof import('~icons/ic/baseline-more-vert')['default']
Icon: typeof import('~icons/ic/on')['default']
IcOutlineInsertDriveFile: typeof import('~icons/ic/outline-insert-drive-file')['default'] IcOutlineInsertDriveFile: typeof import('~icons/ic/outline-insert-drive-file')['default']
IcRoundEdit: typeof import('~icons/ic/round-edit')['default'] IcRoundEdit: typeof import('~icons/ic/round-edit')['default']
IcRoundKeyboardArrowDown: typeof import('~icons/ic/round-keyboard-arrow-down')['default'] IcRoundKeyboardArrowDown: typeof import('~icons/ic/round-keyboard-arrow-down')['default']

8
packages/nc-gui/components/template/Editor.vue

@ -501,16 +501,12 @@ async function importTemplate() {
} }
} }
} }
const createdTable = await $api.base.tableCreate( const createdTable = await $api.base.tableCreate(project.value?.id as string, (baseId || project.value?.bases?.[0].id)!, {
project.value?.id as string,
(baseId || project.value?.bases?.[0].id)!,
{
table_name: table.table_name, table_name: table.table_name,
// leave title empty to get a generated one based on table_name // leave title empty to get a generated one based on table_name
title: '', title: '',
columns: table.columns || [], columns: table.columns || [],
}, })
)
table.id = createdTable.id table.id = createdTable.id
table.title = createdTable.title table.title = createdTable.title

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

@ -40,7 +40,6 @@ export function addAxiosInterceptors(api: Api<any>) {
// Logout user if token refresh didn't work or user is disabled // Logout user if token refresh didn't work or user is disabled
if (error.config.url === '/auth/token/refresh') { if (error.config.url === '/auth/token/refresh') {
state.signOut() state.signOut()
return Promise.reject(error) return Promise.reject(error)
} }

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

@ -28,6 +28,7 @@ export function useGlobalActions(state: State): Actions {
const nuxtApp = useNuxtApp() const nuxtApp = useNuxtApp()
const t = nuxtApp.vueApp.i18n.global.t const t = nuxtApp.vueApp.i18n.global.t
return new Promise((resolve) => {
nuxtApp.$api.instance nuxtApp.$api.instance
.post('/auth/token/refresh', null, { .post('/auth/token/refresh', null, {
withCredentials: true, withCredentials: true,
@ -41,6 +42,8 @@ export function useGlobalActions(state: State): Actions {
message.error(err.message || t('msg.error.youHaveBeenSignedOut')) message.error(err.message || t('msg.error.youHaveBeenSignedOut'))
signOut() signOut()
}) })
.finally(resolve)
})
} }
const loadAppInfo = async () => { const loadAppInfo = async () => {

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

@ -53,7 +53,7 @@ export const useGlobal = createGlobalState((): UseGlobalReturn => {
state.jwtPayload.value.exp && state.jwtPayload.value.exp &&
state.jwtPayload.value.exp - 5 * 60 < state.timestamp.value / 1000 state.jwtPayload.value.exp - 5 * 60 < state.timestamp.value / 1000
), ),
async (expiring) => { async (expiring: boolean) => {
if (getters.signedIn.value && state.jwtPayload.value && expiring) { if (getters.signedIn.value && state.jwtPayload.value && expiring) {
await actions.refreshToken() await actions.refreshToken()
} }

6
packages/nc-gui/middleware/auth.global.ts

@ -52,7 +52,11 @@ export default defineNuxtRouteMiddleware(async (to, from) => {
return navigateTo('/signup') return navigateTo('/signup')
} }
return navigateTo('/signin') /** try generating access token using refresh token */
await state.refreshToken()
/** if user is still not signed in, redirect to signin page */
if (!state.signedIn.value) return navigateTo('/signin')
} else if (to.meta.requiresAuth === false && state.signedIn.value) { } else if (to.meta.requiresAuth === false && state.signedIn.value) {
/** /**
* if user was turned away from non-auth page but also came from a non-auth page (e.g. user went to /signin and reloaded the page) * if user was turned away from non-auth page but also came from a non-auth page (e.g. user went to /signin and reloaded the page)

Loading…
Cancel
Save