From f15bcdeb07e385e263d3d7d2cc05f5153790c5ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fernando=20Fern=C3=A1ndez?= Date: Thu, 24 Oct 2024 18:44:57 +0200 Subject: [PATCH] fix(router): endless recursion in HMR, server selection not available (#2478) --- frontend/src/plugins/router/index.ts | 17 ++++++------ .../src/plugins/router/middlewares/login.ts | 26 ++++++++----------- 2 files changed, 20 insertions(+), 23 deletions(-) diff --git a/frontend/src/plugins/router/index.ts b/frontend/src/plugins/router/index.ts index 9669c45d..31426fa1 100644 --- a/frontend/src/plugins/router/index.ts +++ b/frontend/src/plugins/router/index.ts @@ -1,4 +1,4 @@ -import { watchSyncEffect } from 'vue'; +import { watch } from 'vue'; import { createRouter, createWebHashHistory, @@ -54,19 +54,21 @@ router.back = () => { leave: route.value.meta.layout.transition.leave ?? backTransition }; - const historyState = router.options.history.state; - - if (historyState && isStr(historyState.back)) { + if (isStr(router.options.history.state.back)) { router.go(-1); } else { - router.replace('/'); + void router.replace('/'); } }; /** * Re-run the middleware pipeline when the user logs out or state is cleared */ -watchSyncEffect(() => { +watch([ + () => remote.auth.currentUser, + () => remote.auth.servers, + () => remote.auth.currentServer +], () => { if (!remote.auth.currentUser && remote.auth.servers.length <= 0) { /** * We run the redirect to /server/add as it's the first page in the login flow @@ -91,5 +93,4 @@ watchSyncEffect(() => { ) { void router.replace('/server/select'); } -} -); +}, { flush: 'sync' }); diff --git a/frontend/src/plugins/router/middlewares/login.ts b/frontend/src/plugins/router/middlewares/login.ts index 2b3c6fd1..fb66680a 100644 --- a/frontend/src/plugins/router/middlewares/login.ts +++ b/frontend/src/plugins/router/middlewares/login.ts @@ -36,25 +36,21 @@ export async function loginGuard( } if ( - ( - !jsonConfig.allowServerSelection - && serverRoutes.has(to.path) - ) - || ( - !isNil(remote.auth.currentServer) - && !isNil(remote.auth.currentUser) - && !isNil(remote.auth.currentUserToken) - && routes.has(to.path) - ) + !isNil(remote.auth.currentServer) + && !isNil(remote.auth.currentUser) + && !isNil(remote.auth.currentUserToken) + && routes.has(to.path) ) { return doRedir({ path: '/', replace: true }, to); } - if (!remote.auth.servers.length) { - return doRedir({ path: serverAddUrl, replace: true }, to); - } else if (isNil(remote.auth.currentServer)) { - return doRedir({ path: serverSelectUrl, replace: true }, to); - } else if (isNil(remote.auth.currentUser)) { + if (jsonConfig.allowServerSelection) { + if (!remote.auth.servers.length) { + return doRedir({ path: serverAddUrl, replace: true }, to); + } else if (isNil(remote.auth.currentServer)) { + return doRedir({ path: serverSelectUrl, replace: true }, to); + } + } else { return doRedir({ path: serverLoginUrl, replace: true }, to); }