From 9fcec0864e3bddaadce7f02c33fec5b5ace2bc91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fernando=20Fern=C3=A1ndez?= Date: Fri, 9 Aug 2024 18:09:36 +0200 Subject: [PATCH] refactor: simplify loginGuard logic --- .../src/plugins/router/middlewares/login.ts | 44 ++++++++++--------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/frontend/src/plugins/router/middlewares/login.ts b/frontend/src/plugins/router/middlewares/login.ts index 7e7bb98eb5c..3db51009444 100644 --- a/frontend/src/plugins/router/middlewares/login.ts +++ b/frontend/src/plugins/router/middlewares/login.ts @@ -11,7 +11,8 @@ import { getJSONConfig } from '@/utils/external-config'; const serverAddUrl = '/server/add'; const serverSelectUrl = '/server/select'; const serverLoginUrl = '/server/login'; -const routes = new Set([serverAddUrl, serverSelectUrl, serverLoginUrl]); +const serverRoutes = new Set([serverAddUrl, serverSelectUrl]); +const routes = new Set(...serverRoutes, serverLoginUrl); /** * Redirects to login page if there's no user logged in. @@ -22,28 +23,31 @@ export async function loginGuard( let destinationRoute: RouteLocationPathRaw | undefined; const jsonConfig = await getJSONConfig(); - if (!isNil(remote.auth.currentServer) && !isNil(remote.auth.currentUser) && !isNil(remote.auth.currentUserToken) && routes.has(to.path)) { - destinationRoute = { path: '/', replace: true }; - } else if (to.path === serverAddUrl && remote.auth.servers.length > 0 || to.path === serverSelectUrl) { - if (!jsonConfig.allowServerSelection) { - destinationRoute = { path: serverLoginUrl, replace: true }; - } + if (jsonConfig.defaultServerURLs.length && isNil(remote.auth.currentServer)) { + await until(() => remote.auth.currentServer).toBeTruthy({ flush: 'pre' }); } - if (remote.auth.servers.length <= 0 && jsonConfig.defaultServerURLs.length <= 0) { - destinationRoute = { path: serverAddUrl, replace: true }; - } else if (!routes.has(to.path)) { - if (isNil(remote.auth.currentServer)) { - if (jsonConfig.allowServerSelection) { - destinationRoute = { path: serverSelectUrl, replace: true }; - } else { - await until(() => remote.auth.currentServer).toBeTruthy({ flush: 'pre' }); + if ( + ( + !jsonConfig.allowServerSelection + && serverRoutes.has(to.path) + ) + || ( + !isNil(remote.auth.currentServer) + && !isNil(remote.auth.currentUser) + && !isNil(remote.auth.currentUserToken) + && routes.has(to.path) + ) + ) { + return { path: '/', replace: true }; + } - return loginGuard(to); - } - } else if (isNil(remote.auth.currentUser)) { - destinationRoute = { path: serverLoginUrl, replace: true }; - } + if (!remote.auth.servers.length) { + destinationRoute = { path: serverAddUrl, replace: true }; + } else if (isNil(remote.auth.currentServer)) { + destinationRoute = { path: serverSelectUrl, replace: true }; + } else if (isNil(remote.auth.currentUser)) { + destinationRoute = { path: serverLoginUrl, replace: true }; } return destinationRoute && to.path !== destinationRoute.path