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