1- import { createRouter , createWebHistory } from "vue-router" ;
1+ import { createRouter , createWebHistory , NavigationGuardNext , RouteLocationNormalized } from "vue-router" ;
22import Login from "@admin/views/Login.vue" ;
33import Dashboard from "@admin/views/Dashboard.vue" ;
44import Users from "@admin/views/Users.vue" ;
@@ -7,13 +7,19 @@ import SettingsAuthentication from "@admin/components/Settings/SettingsAuthentic
77import Namespaces from "@admin/views/Namespaces.vue" ;
88import Settings from "@admin/views/Settings.vue" ;
99
10+ import { INotificationsError } from "@admin/interfaces/INotifications" ;
11+ import { computed } from "vue" ;
1012import { store } from "../store" ;
1113
1214const routes = [
1315 {
1416 path : "/login" ,
1517 name : "login" ,
1618 component : Login ,
19+ meta : {
20+ layout : "SimpleLayout" ,
21+ requiresAuth : false ,
22+ } ,
1723 } ,
1824 {
1925 path : "/" ,
@@ -120,21 +126,41 @@ const router = createRouter({
120126 routes,
121127} ) ;
122128
123- router . beforeEach ( ( to , from , next ) => {
124- if ( to . path !== "/login" ) {
125- if ( store . getters [ "auth/isLoggedIn" ] ) {
126- return next ( ) ;
129+ router . beforeEach (
130+ async ( to : RouteLocationNormalized , from : RouteLocationNormalized , next : NavigationGuardNext ) => {
131+ const isLoggedIn : boolean = store . getters [ "auth/isLoggedIn" ] ;
132+ const requiresAuth = to . meta . requiresAuth ?? true ;
133+
134+ const layout = to . meta . layout || "AppLayout" ;
135+ await store . dispatch ( "layout/setLayout" , layout ) ;
136+
137+ if ( ! isLoggedIn && requiresAuth ) {
138+ return next ( {
139+ name : "login" ,
140+ query : { redirect : to . fullPath } ,
141+ } ) ;
127142 }
128- return next ( `/login?redirect=${ to . path } ` ) ;
129- }
130- if ( store . getters [ "auth/isLoggedIn" ] ) {
131- if ( to . path === "/login" && to . query . token ) {
132- return next ( ) ;
143+
144+ if ( isLoggedIn && ! to . meta . requiresAuth ) {
145+ const license = computed ( ( ) => store . getters [ "license/license" ] ) ;
146+
147+ try {
148+ await store . dispatch ( "license/get" ) ;
149+
150+ if ( license . value . expired && to . name !== "SettingLicense" ) {
151+ store . dispatch ( "snackbar/showSnackbarErrorAction" , INotificationsError . license ) ;
152+ return next ( { name : "SettingLicense" } ) ;
153+ }
154+ } catch {
155+ if ( to . name !== "SettingLicense" ) {
156+ store . dispatch ( "snackbar/showSnackbarErrorAction" , INotificationsError . license ) ;
157+ return next ( { name : "SettingLicense" } ) ;
158+ }
159+ }
133160 }
134- return next ( "/" ) ;
135- }
136161
137- return next ( ) ;
138- } ) ;
162+ return next ( ) ;
163+ } ,
164+ ) ;
139165
140166export default router ;
0 commit comments