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