diff --git a/packages/nc-gui-v2/middleware/auth.global.ts b/packages/nc-gui-v2/middleware/auth.global.ts new file mode 100644 index 0000000000..402301bf25 --- /dev/null +++ b/packages/nc-gui-v2/middleware/auth.global.ts @@ -0,0 +1,15 @@ +import { defineNuxtRouteMiddleware, navigateTo, useNuxtApp } from '#app' + +export default defineNuxtRouteMiddleware((to, from) => { + const { $state } = useNuxtApp() + + /** + * By default, we assume that auth is required + * If not required, mark the page as `requiresAuth: false` using `definePageMeta` + */ + if ((to.meta.requiresAuth || typeof to.meta.requiresAuth === 'undefined') && !$state.signedIn.value) { + return navigateTo('/signin') + } else if (to.meta.requiresAuth === false && $state.signedIn.value) { + return navigateTo(from.path) + } +}) diff --git a/packages/nc-gui-v2/pages/signin.vue b/packages/nc-gui-v2/pages/signin.vue index 462264c6c0..6500162b1d 100644 --- a/packages/nc-gui-v2/pages/signin.vue +++ b/packages/nc-gui-v2/pages/signin.vue @@ -1,6 +1,6 @@