From c1eeb3e27d5d3871d49a92c2f80e9fd296d7f4c1 Mon Sep 17 00:00:00 2001 From: Pranav C Date: Mon, 19 Aug 2024 17:10:40 +0000 Subject: [PATCH 1/5] fix: after siginIn redirect to page in which user tried to access --- packages/nc-gui/pages/index.vue | 14 ------------- packages/nc-gui/plugins/redirect.ts | 31 +++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 14 deletions(-) create mode 100644 packages/nc-gui/plugins/redirect.ts diff --git a/packages/nc-gui/pages/index.vue b/packages/nc-gui/pages/index.vue index e08be35db6..51f14edd8f 100644 --- a/packages/nc-gui/pages/index.vue +++ b/packages/nc-gui/pages/index.vue @@ -91,20 +91,6 @@ watch( // immediate watch, because if route is changed during page transition // It will error out nuxt onMounted(() => { - if (route.value.query?.continueAfterSignIn) { - localStorage.removeItem('continueAfterSignIn') - return navigateTo(route.value.query.continueAfterSignIn as string) - } else { - const continueAfterSignIn = localStorage.getItem('continueAfterSignIn') - localStorage.removeItem('continueAfterSignIn') - if (continueAfterSignIn) { - return navigateTo({ - path: continueAfterSignIn, - query: route.value.query, - }) - } - } - handleRouteTypeIdChange().then(() => { if (sharedBaseId.value) { if (!isUIAllowed('baseDuplicate')) { diff --git a/packages/nc-gui/plugins/redirect.ts b/packages/nc-gui/plugins/redirect.ts new file mode 100644 index 0000000000..fcf3dac140 --- /dev/null +++ b/packages/nc-gui/plugins/redirect.ts @@ -0,0 +1,31 @@ +// this plugin is used to redirect user to the page they were trying to access before they were redirected to the login page +export default defineNuxtPlugin(function (nuxtApp) { + const router = useRouter() + + const route = router.currentRoute + + watch( + () => (nuxtApp.$state as ReturnType)?.token?.value, + async (newToken, oldToken) => { + try { + if (newToken && newToken !== oldToken) { + if (route.value.query?.continueAfterSignIn) { + localStorage.removeItem('continueAfterSignIn') + await navigateTo(route.value.query.continueAfterSignIn as string) + } else { + const continueAfterSignIn = localStorage.getItem('continueAfterSignIn') + if (continueAfterSignIn) { + localStorage.removeItem('continueAfterSignIn') + await navigateTo({ + path: continueAfterSignIn, + query: route.value.query, + }) + } + } + } + } catch (e) { + console.error(e) + } + }, + ) +}) From 33cc226e81008cc2d9d7d4c40ca6b60f5b44ab5b Mon Sep 17 00:00:00 2001 From: Pranav C Date: Mon, 19 Aug 2024 17:10:40 +0000 Subject: [PATCH 2/5] fix: set redirect info to local storage --- packages/nc-gui/middleware/02.auth.global.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/nc-gui/middleware/02.auth.global.ts b/packages/nc-gui/middleware/02.auth.global.ts index a2be572337..9d180bc354 100644 --- a/packages/nc-gui/middleware/02.auth.global.ts +++ b/packages/nc-gui/middleware/02.auth.global.ts @@ -80,6 +80,7 @@ export default defineNuxtRouteMiddleware(async (to, from) => { /** if user is still not signed in, redirect to signin page */ if (!state.signedIn.value) { + localStorage.setItem('continueAfterSignIn', to.fullPath) return navigateTo({ path: '/signin', query: to.fullPath !== '/' && to.fullPath.match(/^\/(?!\?)/) ? { continueAfterSignIn: to.fullPath } : {}, From 1298a49aebcf1619d35c326b90281ba02c08471e Mon Sep 17 00:00:00 2001 From: Pranav C Date: Mon, 19 Aug 2024 17:10:40 +0000 Subject: [PATCH 3/5] fix: proper redirection after login --- packages/nc-gui/plugins/redirect.ts | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/packages/nc-gui/plugins/redirect.ts b/packages/nc-gui/plugins/redirect.ts index fcf3dac140..b62f465d5b 100644 --- a/packages/nc-gui/plugins/redirect.ts +++ b/packages/nc-gui/plugins/redirect.ts @@ -9,18 +9,17 @@ export default defineNuxtPlugin(function (nuxtApp) { async (newToken, oldToken) => { try { if (newToken && newToken !== oldToken) { - if (route.value.query?.continueAfterSignIn) { - localStorage.removeItem('continueAfterSignIn') - await navigateTo(route.value.query.continueAfterSignIn as string) - } else { - const continueAfterSignIn = localStorage.getItem('continueAfterSignIn') - if (continueAfterSignIn) { - localStorage.removeItem('continueAfterSignIn') - await navigateTo({ - path: continueAfterSignIn, - query: route.value.query, - }) + try { + if (route.value.query?.continueAfterSignIn) { + await navigateTo(route.value.query.continueAfterSignIn as string) + } else { + const continueAfterSignIn = localStorage.getItem('continueAfterSignIn') + if (continueAfterSignIn) { + await navigateTo(continueAfterSignIn) + } } + } finally { + localStorage.removeItem('continueAfterSignIn') } } } catch (e) { From 10e478ad6b80e0c10b2e49ea81ee152024fabc17 Mon Sep 17 00:00:00 2001 From: Pranav C Date: Mon, 19 Aug 2024 17:10:40 +0000 Subject: [PATCH 4/5] refactor: use useGlobal composable to get token --- packages/nc-gui/plugins/redirect.ts | 44 ++++++++++++++++------------- 1 file changed, 25 insertions(+), 19 deletions(-) diff --git a/packages/nc-gui/plugins/redirect.ts b/packages/nc-gui/plugins/redirect.ts index b62f465d5b..018a8df656 100644 --- a/packages/nc-gui/plugins/redirect.ts +++ b/packages/nc-gui/plugins/redirect.ts @@ -4,27 +4,33 @@ export default defineNuxtPlugin(function (nuxtApp) { const route = router.currentRoute - watch( - () => (nuxtApp.$state as ReturnType)?.token?.value, - async (newToken, oldToken) => { - try { - if (newToken && newToken !== oldToken) { - try { - if (route.value.query?.continueAfterSignIn) { - await navigateTo(route.value.query.continueAfterSignIn as string) - } else { - const continueAfterSignIn = localStorage.getItem('continueAfterSignIn') - if (continueAfterSignIn) { - await navigateTo(continueAfterSignIn) + // put inside app:created hook to ensure global state is available + nuxtApp.hooks.hook('app:created', () => { + const { token } = useGlobal() + + watch( + () => token.value ?? (nuxtApp.$state as ReturnType)?.token?.value, + async (newToken, oldToken) => { + try { + if (newToken && newToken !== oldToken) { + try { + if (route.value.query?.continueAfterSignIn) { + await navigateTo(route.value.query.continueAfterSignIn as string) + } else { + const continueAfterSignIn = localStorage.getItem('continueAfterSignIn') + if (continueAfterSignIn) { + await navigateTo(continueAfterSignIn) + } } + } finally { + localStorage.removeItem('continueAfterSignIn') } - } finally { - localStorage.removeItem('continueAfterSignIn') } + } catch (e) { + console.error(e) } - } catch (e) { - console.error(e) - } - }, - ) + }, + { immediate: true }, + ) + }) }) From 6b2d2ec80ce1c5e9bafeb2682a6790fbfb9eaa18 Mon Sep 17 00:00:00 2001 From: Pranav C Date: Mon, 19 Aug 2024 17:10:40 +0000 Subject: [PATCH 5/5] fix: handle redirect query param --- packages/nc-gui/plugins/redirect.ts | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/packages/nc-gui/plugins/redirect.ts b/packages/nc-gui/plugins/redirect.ts index 018a8df656..ed598240a3 100644 --- a/packages/nc-gui/plugins/redirect.ts +++ b/packages/nc-gui/plugins/redirect.ts @@ -4,10 +4,22 @@ export default defineNuxtPlugin(function (nuxtApp) { const route = router.currentRoute + // watch for continueAfterSignIn query param and store it in localStorage so that it can be used after sign in + watch( + () => route.value.query?.continueAfterSignIn, + (continueAfterSignIn) => { + if (continueAfterSignIn) { + localStorage.setItem('continueAfterSignIn', continueAfterSignIn as string) + } + }, + { + immediate: true, + }, + ) + // put inside app:created hook to ensure global state is available nuxtApp.hooks.hook('app:created', () => { const { token } = useGlobal() - watch( () => token.value ?? (nuxtApp.$state as ReturnType)?.token?.value, async (newToken, oldToken) => {