From a17b0e329e97e2bda910526dbb7fcbdc124fa722 Mon Sep 17 00:00:00 2001 From: Michael Genson <71845777+michael-genson@users.noreply.github.com> Date: Mon, 6 Oct 2025 13:02:25 -0500 Subject: [PATCH] fix: No Redirect On Valid Token (#6327) --- frontend/composables/useAuthBackend.ts | 12 ++---------- frontend/pages/admin/setup.vue | 8 +------- frontend/plugins/init-auth.client.ts | 9 +++++++++ 3 files changed, 12 insertions(+), 17 deletions(-) create mode 100644 frontend/plugins/init-auth.client.ts diff --git a/frontend/composables/useAuthBackend.ts b/frontend/composables/useAuthBackend.ts index 1d7660514..9eec3b29a 100644 --- a/frontend/composables/useAuthBackend.ts +++ b/frontend/composables/useAuthBackend.ts @@ -20,7 +20,7 @@ interface AuthState { } const authUser = ref(null); -const authStatus = ref<"loading" | "authenticated" | "unauthenticated">("unauthenticated"); +const authStatus = ref<"loading" | "authenticated" | "unauthenticated">("loading"); export const useAuthBackend = function (): AuthState { const { $axios } = useNuxtApp(); @@ -42,7 +42,6 @@ export const useAuthBackend = function (): AuthState { router.push("/login"); } } - return false; } async function getSession(): Promise { @@ -59,9 +58,9 @@ export const useAuthBackend = function (): AuthState { authStatus.value = "authenticated"; } catch (error: any) { + console.error("Failed to fetch user session:", error); handleAuthError(error); authStatus.value = "unauthenticated"; - throw error; } } @@ -140,13 +139,6 @@ export const useAuthBackend = function (): AuthState { }, { immediate: true }); } - // Initialize auth state if token exists - if (import.meta.client && tokenCookie.value && authStatus.value === "unauthenticated") { - getSession().catch((error: any) => { - handleAuthError(error); - }); - } - return { data: computed(() => authUser.value), status: computed(() => authStatus.value), diff --git a/frontend/pages/admin/setup.vue b/frontend/pages/admin/setup.vue index 5e420fbf1..a846d3416 100644 --- a/frontend/pages/admin/setup.vue +++ b/frontend/pages/admin/setup.vue @@ -244,6 +244,7 @@ import UserRegistrationForm from "~/components/Domain/User/UserRegistrationForm. definePageMeta({ layout: "blank", + middleware: ["admin-only"], }); // ================================================================ @@ -264,13 +265,6 @@ useSeoMeta({ title: i18n.t("admin.setup.first-time-setup"), }); -if (!$auth.loggedIn.value) { - router.push("/login"); -} -else if (!$auth.user.value?.admin) { - router.push(groupSlug.value ? `/g/${groupSlug.value}` : "/login"); -} - enum Pages { LANDING = 0, USER_INFO = 1, diff --git a/frontend/plugins/init-auth.client.ts b/frontend/plugins/init-auth.client.ts new file mode 100644 index 000000000..f1f2cf209 --- /dev/null +++ b/frontend/plugins/init-auth.client.ts @@ -0,0 +1,9 @@ +export default defineNuxtPlugin({ + async setup() { + const auth = useAuthBackend(); + + console.debug("Initializing auth plugin"); + await auth.getSession(); + console.debug("Auth plugin initialized"); + }, +});