@@ -11,42 +11,52 @@ import { getJSONConfig } from '@/utils/external-config';
11
11
const serverAddUrl = '/server/add' ;
12
12
const serverSelectUrl = '/server/select' ;
13
13
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
+ }
15
25
16
26
/**
17
27
* Redirects to login page if there's no user logged in.
18
28
*/
19
29
export async function loginGuard (
20
30
to : RouteLocationNormalized
21
31
) : Promise < boolean | RouteLocationRaw > {
22
- let destinationRoute : RouteLocationPathRaw | undefined ;
23
32
const jsonConfig = await getJSONConfig ( ) ;
24
33
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' } ) ;
31
36
}
32
37
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
+ }
41
52
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 ) ;
47
59
}
48
60
49
- return destinationRoute && to . path !== destinationRoute . path
50
- ? destinationRoute
51
- : true ;
61
+ return true ;
52
62
}
0 commit comments