Skip to content

Commit 5f4249b

Browse files
committed
refactor: simplify loginGuard logic
1 parent 961f242 commit 5f4249b

File tree

1 file changed

+34
-24
lines changed
  • frontend/src/plugins/router/middlewares

1 file changed

+34
-24
lines changed

frontend/src/plugins/router/middlewares/login.ts

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,42 +11,52 @@ import { getJSONConfig } from '@/utils/external-config';
1111
const serverAddUrl = '/server/add';
1212
const serverSelectUrl = '/server/select';
1313
const serverLoginUrl = '/server/login';
14-
const routes = new Set([serverAddUrl, serverSelectUrl, serverLoginUrl]);
14+
const serverRoutes = new Set([serverAddUrl, serverSelectUrl]);
15+
const routes = new Set([...serverRoutes, serverLoginUrl]);
16+
17+
/**
18+
* Performs the login guard redirection ensuring no redirection loops happen
19+
*/
20+
function doRedir(dest: RouteLocationPathRaw, to: RouteLocationNormalized) {
21+
return to.path === dest.path
22+
? true
23+
: dest;
24+
}
1525

1626
/**
1727
* Redirects to login page if there's no user logged in.
1828
*/
1929
export async function loginGuard(
2030
to: RouteLocationNormalized
2131
): Promise<boolean | RouteLocationRaw> {
22-
let destinationRoute: RouteLocationPathRaw | undefined;
2332
const jsonConfig = await getJSONConfig();
2433

25-
if (!isNil(remote.auth.currentServer) && !isNil(remote.auth.currentUser) && !isNil(remote.auth.currentUserToken) && routes.has(to.path)) {
26-
destinationRoute = { path: '/', replace: true };
27-
} else if (to.path === serverAddUrl && remote.auth.servers.length > 0 || to.path === serverSelectUrl) {
28-
if (!jsonConfig.allowServerSelection) {
29-
destinationRoute = { path: serverLoginUrl, replace: true };
30-
}
34+
if (jsonConfig.defaultServerURLs.length && isNil(remote.auth.currentServer)) {
35+
await until(() => remote.auth.currentServer).toBeTruthy({ flush: 'pre' });
3136
}
3237

33-
if (remote.auth.servers.length <= 0 && jsonConfig.defaultServerURLs.length <= 0) {
34-
destinationRoute = { path: serverAddUrl, replace: true };
35-
} else if (!routes.has(to.path)) {
36-
if (isNil(remote.auth.currentServer)) {
37-
if (jsonConfig.allowServerSelection) {
38-
destinationRoute = { path: serverSelectUrl, replace: true };
39-
} else {
40-
await until(() => remote.auth.currentServer).toBeTruthy({ flush: 'pre' });
38+
if (
39+
(
40+
!jsonConfig.allowServerSelection
41+
&& serverRoutes.has(to.path)
42+
)
43+
|| (
44+
!isNil(remote.auth.currentServer)
45+
&& !isNil(remote.auth.currentUser)
46+
&& !isNil(remote.auth.currentUserToken)
47+
&& routes.has(to.path)
48+
)
49+
) {
50+
return doRedir({ path: '/', replace: true }, to);
51+
}
4152

42-
return loginGuard(to);
43-
}
44-
} else if (isNil(remote.auth.currentUser)) {
45-
destinationRoute = { path: serverLoginUrl, replace: true };
46-
}
53+
if (!remote.auth.servers.length) {
54+
return doRedir({ path: serverAddUrl, replace: true }, to);
55+
} else if (isNil(remote.auth.currentServer)) {
56+
return doRedir({ path: serverSelectUrl, replace: true }, to);
57+
} else if (isNil(remote.auth.currentUser)) {
58+
return doRedir({ path: serverLoginUrl, replace: true }, to);
4759
}
4860

49-
return destinationRoute && to.path !== destinationRoute.path
50-
? destinationRoute
51-
: true;
61+
return true;
5262
}

0 commit comments

Comments
 (0)