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 }, + ) + }) })