@@ -36,54 +36,34 @@ export function createRouteGuard(router: Router) {
3636 const routeRoles = to . meta . roles || [ ] ;
3737
3838 const hasRole = authStore . userInfo . roles . some ( role => routeRoles . includes ( role ) ) ;
39-
4039 const hasAuth = authStore . isStaticSuper || ! routeRoles . length || hasRole ;
4140
42- const routeSwitches : CommonType . StrategicPattern [ ] = [
43- // if it is login route when logged in, then switch to the root page
44- {
45- condition : isLogin && to . name === loginRoute ,
46- callback : ( ) => {
47- next ( { name : rootRoute } ) ;
48- }
49- } ,
50- // if it is constant route, then it is allowed to access directly
51- {
52- condition : ! needLogin ,
53- callback : ( ) => {
54- handleRouteSwitch ( to , from , next ) ;
55- }
56- } ,
57- // if the route need login but the user is not logged in, then switch to the login page
58- {
59- condition : ! isLogin && needLogin ,
60- callback : ( ) => {
61- next ( { name : loginRoute , query : { redirect : to . fullPath } } ) ;
62- }
63- } ,
64- // if the user is logged in and has authorization, then it is allowed to access
65- {
66- condition : isLogin && needLogin && hasAuth ,
67- callback : ( ) => {
68- handleRouteSwitch ( to , from , next ) ;
69- }
70- } ,
71- // if the user is logged in but does not have authorization, then switch to the 403 page
72- {
73- condition : isLogin && needLogin && ! hasAuth ,
74- callback : ( ) => {
75- next ( { name : noAuthorizationRoute } ) ;
76- }
77- }
78- ] ;
79-
80- routeSwitches . some ( ( { condition, callback } ) => {
81- if ( condition ) {
82- callback ( ) ;
83- }
84-
85- return condition ;
86- } ) ;
41+ // if it is login route when logged in, then switch to the root page
42+ if ( to . name === loginRoute && isLogin ) {
43+ next ( { name : rootRoute } ) ;
44+ return ;
45+ }
46+
47+ // if the route does not need login, then it is allowed to access directly
48+ if ( ! needLogin ) {
49+ handleRouteSwitch ( to , from , next ) ;
50+ return ;
51+ }
52+
53+ // the route need login but the user is not logged in, then switch to the login page
54+ if ( ! isLogin ) {
55+ next ( { name : loginRoute , query : { redirect : to . fullPath } } ) ;
56+ return ;
57+ }
58+
59+ // if the user is logged in but does not have authorization, then switch to the 403 page
60+ if ( ! hasAuth ) {
61+ next ( { name : noAuthorizationRoute } ) ;
62+ return ;
63+ }
64+
65+ // switch route normally
66+ handleRouteSwitch ( to , from , next ) ;
8767 } ) ;
8868}
8969
@@ -93,7 +73,6 @@ export function createRouteGuard(router: Router) {
9373 * @param to to route
9474 */
9575async function initRoute ( to : RouteLocationNormalized ) : Promise < RouteLocationRaw | null > {
96- const authStore = useAuthStore ( ) ;
9776 const routeStore = useRouteStore ( ) ;
9877
9978 const notFoundRoute : RouteKey = 'not-found' ;
@@ -105,50 +84,28 @@ async function initRoute(to: RouteLocationNormalized): Promise<RouteLocationRaw
10584
10685 // the route is captured by the "not-found" route because the constant route is not initialized
10786 // after the constant route is initialized, redirect to the original route
108- if ( isNotFoundRoute ) {
109- const path = to . fullPath ;
110-
111- const location : RouteLocationRaw = {
112- path,
113- replace : true ,
114- query : to . query ,
115- hash : to . hash
116- } ;
117-
118- return location ;
119- }
120- }
87+ const path = to . fullPath ;
88+ const location : RouteLocationRaw = {
89+ path,
90+ replace : true ,
91+ query : to . query ,
92+ hash : to . hash
93+ } ;
12194
122- // if the route is the constant route but is not the "not-found" route, then it is allowed to access.
123- if ( to . meta . constant && ! isNotFoundRoute ) {
124- return null ;
95+ return location ;
12596 }
12697
127- // the auth route is initialized
128- // it is not the "not-found" route, then it is allowed to access
129- if ( routeStore . isInitAuthRoute && ! isNotFoundRoute ) {
130- return null ;
131- }
132- // it is captured by the "not-found" route, then check whether the route exists
133- if ( routeStore . isInitAuthRoute && isNotFoundRoute ) {
134- const exist = await routeStore . getIsAuthRouteExist ( to . path as RoutePath ) ;
135- const noPermissionRoute : RouteKey = '403' ;
98+ const isLogin = Boolean ( localStg . get ( 'token' ) ) ;
13699
137- if ( exist ) {
138- const location : RouteLocationRaw = {
139- name : noPermissionRoute
140- } ;
100+ if ( ! isLogin ) {
101+ // if the user is not logged in and the route is a constant route but not the "not-found" route, then it is allowed to access.
102+ if ( to . meta . constant && ! isNotFoundRoute ) {
103+ routeStore . onRouteSwitchWhenNotLoggedIn ( ) ;
141104
142- return location ;
105+ return null ;
143106 }
144107
145- return null ;
146- }
147-
148- // if the auth route is not initialized, then initialize the auth route
149- const isLogin = Boolean ( localStg . get ( 'token' ) ) ;
150- // initialize the auth route requires the user to be logged in, if not, redirect to the login page
151- if ( ! isLogin ) {
108+ // if the user is not logged in, then switch to the login page
152109 const loginRoute : RouteKey = 'login' ;
153110 const query = getRouteQueryOfLoginRoute ( to , routeStore . routeHome ) ;
154111
@@ -160,22 +117,42 @@ async function initRoute(to: RouteLocationNormalized): Promise<RouteLocationRaw
160117 return location ;
161118 }
162119
163- await authStore . initUserInfo ( ) ;
120+ if ( ! routeStore . isInitAuthRoute ) {
121+ // initialize the auth route
122+ await routeStore . initAuthRoute ( ) ;
164123
165- // initialize the auth route
166- await routeStore . initAuthRoute ( ) ;
124+ // the route is captured by the "not-found" route because the auth route is not initialized
125+ // after the auth route is initialized, redirect to the original route
126+ if ( isNotFoundRoute ) {
127+ const rootRoute : RouteKey = 'root' ;
128+ const path = to . redirectedFrom ?. name === rootRoute ? '/' : to . fullPath ;
167129
168- // the route is captured by the "not-found" route because the auth route is not initialized
169- // after the auth route is initialized, redirect to the original route
170- if ( isNotFoundRoute ) {
171- const rootRoute : RouteKey = 'root' ;
172- const path = to . redirectedFrom ?. name === rootRoute ? '/' : to . fullPath ;
130+ const location : RouteLocationRaw = {
131+ path,
132+ replace : true ,
133+ query : to . query ,
134+ hash : to . hash
135+ } ;
136+
137+ return location ;
138+ }
139+ }
140+
141+ // the auth route is initialized
142+ // it is not the "not-found" route, then it is allowed to access
143+ if ( ! isNotFoundRoute ) {
144+ routeStore . onRouteSwitchWhenLoggedIn ( ) ;
173145
146+ return null ;
147+ }
148+
149+ // it is captured by the "not-found" route, then check whether the route exists
150+ const exist = await routeStore . getIsAuthRouteExist ( to . path as RoutePath ) ;
151+ const noPermissionRoute : RouteKey = '403' ;
152+
153+ if ( exist ) {
174154 const location : RouteLocationRaw = {
175- path,
176- replace : true ,
177- query : to . query ,
178- hash : to . hash
155+ name : noPermissionRoute
179156 } ;
180157
181158 return location ;
0 commit comments