Browse Source

refactor: use useGlobal composable to get token

pull/9294/head
Pranav C 4 months ago
parent
commit
10e478ad6b
  1. 44
      packages/nc-gui/plugins/redirect.ts

44
packages/nc-gui/plugins/redirect.ts

@ -4,27 +4,33 @@ export default defineNuxtPlugin(function (nuxtApp) {
const route = router.currentRoute
watch(
() => (nuxtApp.$state as ReturnType<typeof useGlobal>)?.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<typeof useGlobal>)?.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 },
)
})
})

Loading…
Cancel
Save