From 1d7f5235b032c9cf33643e4afe5f2be8c50fe531 Mon Sep 17 00:00:00 2001 From: Pranav C Date: Mon, 23 Sep 2024 10:12:15 +0000 Subject: [PATCH 1/4] fix: redirect only in page where signin action triggers --- packages/nc-gui/composables/useGlobal/actions.ts | 3 +++ packages/nc-gui/plugins/redirect.ts | 4 +++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/nc-gui/composables/useGlobal/actions.ts b/packages/nc-gui/composables/useGlobal/actions.ts index 1b077f9aa2..5187b77d2f 100644 --- a/packages/nc-gui/composables/useGlobal/actions.ts +++ b/packages/nc-gui/composables/useGlobal/actions.ts @@ -3,6 +3,8 @@ import type { Actions, AppInfo, State } from './types' import type { NcProjectType } from '#imports' export function useGlobalActions(state: State): Actions { + const isTokenUpdatedTab = useState('isTokenUpdatedTab', () => false) + const setIsMobileMode = (isMobileMode: boolean) => { state.isMobileMode.value = isMobileMode } @@ -44,6 +46,7 @@ export function useGlobalActions(state: State): Actions { * keepProps - is for keeping any existing role info if user id is same as previous user * */ const signIn: Actions['signIn'] = async (newToken, keepProps = false) => { + isTokenUpdatedTab.value = true state.token.value = newToken if (state.jwtPayload.value) { diff --git a/packages/nc-gui/plugins/redirect.ts b/packages/nc-gui/plugins/redirect.ts index 1bf5369f08..ded64883fe 100644 --- a/packages/nc-gui/plugins/redirect.ts +++ b/packages/nc-gui/plugins/redirect.ts @@ -4,6 +4,7 @@ const isFullUrl = (url: string) => { // 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 isTokenUpdatedTab = useState('isTokenUpdatedTab', () => false) const router = useRouter() const route = router.currentRoute @@ -28,7 +29,7 @@ export default defineNuxtPlugin(function (nuxtApp) { () => token.value ?? (nuxtApp.$state as ReturnType)?.token?.value, async (newToken, oldToken) => { try { - if (newToken && newToken !== oldToken) { + if (newToken && newToken !== oldToken && isTokenUpdatedTab.value) { try { if (route.value.query?.continueAfterSignIn) { await navigateTo(route.value.query.continueAfterSignIn as string, { @@ -44,6 +45,7 @@ export default defineNuxtPlugin(function (nuxtApp) { } } finally { localStorage.removeItem('continueAfterSignIn') + isTokenUpdatedTab.value = false } } } catch (e) { From 245fd3262d8f66a5474e2d9d7976b073df816884 Mon Sep 17 00:00:00 2001 From: Pranav C Date: Mon, 23 Sep 2024 10:12:16 +0000 Subject: [PATCH 2/4] refactor: redirect even if --- packages/nc-gui/plugins/redirect.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/packages/nc-gui/plugins/redirect.ts b/packages/nc-gui/plugins/redirect.ts index ded64883fe..cfa3bd5a8e 100644 --- a/packages/nc-gui/plugins/redirect.ts +++ b/packages/nc-gui/plugins/redirect.ts @@ -24,12 +24,14 @@ export default defineNuxtPlugin(function (nuxtApp) { // put inside app:created hook to ensure global state is available nuxtApp.hooks.hook('app:created', () => { - const { token } = useGlobal() + const {token} = useGlobal() watch( () => token.value ?? (nuxtApp.$state as ReturnType)?.token?.value, async (newToken, oldToken) => { try { - if (newToken && newToken !== oldToken && isTokenUpdatedTab.value) { + // if token updated and continueAfterSignIn query param is present, redirect to that page + // or if token updated and isTokenUpdatedTab is true, redirect to the page stored in localStorage + if (newToken && newToken !== oldToken && (isTokenUpdatedTab.value || route.value.query?.continueAfterSignIn)) { try { if (route.value.query?.continueAfterSignIn) { await navigateTo(route.value.query.continueAfterSignIn as string, { @@ -52,7 +54,7 @@ export default defineNuxtPlugin(function (nuxtApp) { console.error(e) } }, - { immediate: true }, + {immediate: true}, ) }) }) From e215d5781b1bf3285ba60fb3381f0e460204e6b2 Mon Sep 17 00:00:00 2001 From: Pranav C Date: Mon, 23 Sep 2024 10:12:16 +0000 Subject: [PATCH 3/4] refactor: redirect even if --- packages/nc-gui/plugins/redirect.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/nc-gui/plugins/redirect.ts b/packages/nc-gui/plugins/redirect.ts index cfa3bd5a8e..20801a662c 100644 --- a/packages/nc-gui/plugins/redirect.ts +++ b/packages/nc-gui/plugins/redirect.ts @@ -29,8 +29,9 @@ export default defineNuxtPlugin(function (nuxtApp) { () => token.value ?? (nuxtApp.$state as ReturnType)?.token?.value, async (newToken, oldToken) => { try { - // if token updated and continueAfterSignIn query param is present, redirect to that page - // or if token updated and isTokenUpdatedTab is true, redirect to the page stored in localStorage + // if token updated redirect if one of the following condition matches, + // 1. `continueAfterSignIn` query param is present in the url + // 2. If signin happened in current tab which can be detected by `isTokenUpdatedTab` flag if (newToken && newToken !== oldToken && (isTokenUpdatedTab.value || route.value.query?.continueAfterSignIn)) { try { if (route.value.query?.continueAfterSignIn) { From 557be169076bd2b5de0be4dd8f95177fb5b6f898 Mon Sep 17 00:00:00 2001 From: Pranav C Date: Mon, 23 Sep 2024 10:12:16 +0000 Subject: [PATCH 4/4] chore: lint --- packages/nc-gui/plugins/redirect.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/nc-gui/plugins/redirect.ts b/packages/nc-gui/plugins/redirect.ts index 20801a662c..1c5c1da28f 100644 --- a/packages/nc-gui/plugins/redirect.ts +++ b/packages/nc-gui/plugins/redirect.ts @@ -24,7 +24,7 @@ export default defineNuxtPlugin(function (nuxtApp) { // put inside app:created hook to ensure global state is available nuxtApp.hooks.hook('app:created', () => { - const {token} = useGlobal() + const { token } = useGlobal() watch( () => token.value ?? (nuxtApp.$state as ReturnType)?.token?.value, async (newToken, oldToken) => { @@ -55,7 +55,7 @@ export default defineNuxtPlugin(function (nuxtApp) { console.error(e) } }, - {immediate: true}, + { immediate: true }, ) }) })