@@ -31,11 +31,6 @@ const { user, hasRole, hasPermission, signIn, signOut } = useAuth()
3131
3232export { user , hasRole , hasPermission }
3333
34- checkAuth ( ) . then ( auth => {
35- if ( auth ) signIn ( auth )
36- else signOut ( )
37- } )
38-
3934export async function revalidate ( ) {
4035 loading . value = true
4136 const auth = await checkAuth ( )
@@ -55,25 +50,15 @@ export const signout = async (router:any, redirectTo?: string) => {
5550 await router . replace ( { path : redirectTo ?? router ?. currentRoute ?. value . path , force : true } )
5651}
5752
58-
59- // Use Route Guards to guard against access to pages
60- type RouteGuard = { path :string , attr :string }
61- const routes :RouteGuard [ ] = [
62- { path :'/profile' , attr :'auth' } ,
63- { path :'/admin' , attr :'role:Admin' } ,
64- { path :'/bookings' , attr :'role:Employee' } ,
65- { path :'/bookings-crud' , attr :'role:Employee' } ,
66- ]
67-
6853export function configRouter ( router :Router ) {
69- const invalidAttrRedirect = ( to :RouteLocationNormalized , guardAttr :string , userAttrs :string [ ] ) => userAttrs . indexOf ( 'auth' ) === - 1
54+ const invalidAttrRedirect = ( to :RouteLocationNormalized , _ :string , userAttrs :string [ ] ) => userAttrs . indexOf ( 'auth' ) === - 1
7055 ? Routes . signin ( to . path )
7156 : Routes . forbidden ( )
7257
7358 // Validate Route guards against Authenticated User's Attributes
7459 const validateRoute = ( to :RouteLocationNormalized , next :NavigationGuardNext , attrs :string [ ] ) => {
75- for ( let i = 0 ; i < routes . length ; i ++ ) {
76- const route = routes [ i ]
60+ for ( let i = 0 ; i < Routes . guards . length ; i ++ ) {
61+ const route = Routes . guards [ i ]
7762 if ( ! route ) continue
7863 const { path, attr } = route
7964 if ( ! to . path . startsWith ( path ) ) continue
@@ -82,6 +67,7 @@ export function configRouter(router:Router) {
8267 const allowAdmin = isAdmin && ( attr . startsWith ( 'role:' ) || attr . startsWith ( 'perm:' ) )
8368 if ( ! allowAdmin ) {
8469 const goTo = invalidAttrRedirect ( to , attr , attrs )
70+ console . debug ( `Redirecting to ${ goTo } as missing required '${ attr } ' to access '${ to . path } '` )
8571 next ( goTo )
8672 return
8773 }
@@ -90,7 +76,7 @@ export function configRouter(router:Router) {
9076 next ( )
9177 }
9278
93- router . beforeEach ( ( to :RouteLocationNormalized , from :RouteLocationNormalized , next :NavigationGuardNext ) => {
79+ router . beforeEach ( ( to :RouteLocationNormalized , _ :RouteLocationNormalized , next :NavigationGuardNext ) => {
9480 if ( loading ) {
9581 const stop = watchEffect ( ( ) => {
9682 validateRoute ( to , next , attrs . value )
0 commit comments