File tree Expand file tree Collapse file tree 2 files changed +9
-5
lines changed
Expand file tree Collapse file tree 2 files changed +9
-5
lines changed Original file line number Diff line number Diff line change @@ -12,21 +12,25 @@ const router = createRouter({
1212
1313router . beforeEach ( async ( to , from , next ) => {
1414 const authStore = useAuthStore ( ) ;
15+ const requiresAbility = to ?. meta ?. requiresAbility ;
16+ const requiresAuth = to ?. meta ?. requiresAuth ;
17+ const belongsToOwnerOnly = to ?. meta ?. isOwner ;
18+
1519 if ( ! authStore . user ) {
1620 await authStore . getCurrentUser ( ) ;
1721 }
1822 if ( ! authStore . user ) {
1923 authStore . clearBrowserData ( ) ;
24+ if ( requiresAuth ) {
25+ next ( { name : 'login' } )
26+ }
2027 }
2128
2229 if ( to ?. meta ?. isPublicAuthPage && authStore . user ) {
2330 next ( { name : 'dashboard' } )
2431 return ;
2532 }
2633
27- const requiresAbility = to . meta . requiresAbility ;
28- const requiresAuth = to . meta . requiresAuth ;
29- const belongsToOwnerOnly = to . meta . isOwner ;
3034 if ( requiresAbility && requiresAuth ) {
3135 if ( authStore . hasAbilities ( requiresAbility ) ) {
3236 next ( )
Original file line number Diff line number Diff line change @@ -103,7 +103,7 @@ export const useAuthStore = defineStore("auth", {
103103 window . localStorage . removeItem ( 'currentUser' ) ;
104104 } ,
105105 hasAbilities ( abilities ) {
106- return this . user . hasOwnProperty ( 'abilities' ) && ! ! this . user . abilities . find ( ( ab ) => {
106+ return this . user && this . user . hasOwnProperty ( 'abilities' ) && ! ! this . user . abilities . find ( ( ab ) => {
107107 if ( ab . name === '*' ) {
108108 return true
109109 }
@@ -118,7 +118,7 @@ export const useAuthStore = defineStore("auth", {
118118
119119 hasAllAbilities ( abilities ) {
120120 let isAvailable = true
121- if ( this . user . hasOwnProperty ( 'abilities' ) ) {
121+ if ( this . user && this . user . hasOwnProperty ( 'abilities' ) ) {
122122 this . user . abilities . filter ( ( ab ) => {
123123 let hasContain = ! ! abilities . find ( ( p ) => {
124124 return ab . name === p
You can’t perform that action at this time.
0 commit comments