From c1eeb3e27d5d3871d49a92c2f80e9fd296d7f4c1 Mon Sep 17 00:00:00 2001 From: Pranav C Date: Mon, 19 Aug 2024 17:10:40 +0000 Subject: [PATCH] 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) + } + }, + ) +})