File tree 2 files changed +9
-5
lines changed
2 files changed +9
-5
lines changed Original file line number Diff line number Diff line change @@ -12,21 +12,25 @@ const router = createRouter({
12
12
13
13
router . beforeEach ( async ( to , from , next ) => {
14
14
const authStore = useAuthStore ( ) ;
15
+ const requiresAbility = to ?. meta ?. requiresAbility ;
16
+ const requiresAuth = to ?. meta ?. requiresAuth ;
17
+ const belongsToOwnerOnly = to ?. meta ?. isOwner ;
18
+
15
19
if ( ! authStore . user ) {
16
20
await authStore . getCurrentUser ( ) ;
17
21
}
18
22
if ( ! authStore . user ) {
19
23
authStore . clearBrowserData ( ) ;
24
+ if ( requiresAuth ) {
25
+ next ( { name : 'login' } )
26
+ }
20
27
}
21
28
22
29
if ( to ?. meta ?. isPublicAuthPage && authStore . user ) {
23
30
next ( { name : 'dashboard' } )
24
31
return ;
25
32
}
26
33
27
- const requiresAbility = to . meta . requiresAbility ;
28
- const requiresAuth = to . meta . requiresAuth ;
29
- const belongsToOwnerOnly = to . meta . isOwner ;
30
34
if ( requiresAbility && requiresAuth ) {
31
35
if ( authStore . hasAbilities ( requiresAbility ) ) {
32
36
next ( )
Original file line number Diff line number Diff line change @@ -103,7 +103,7 @@ export const useAuthStore = defineStore("auth", {
103
103
window . localStorage . removeItem ( 'currentUser' ) ;
104
104
} ,
105
105
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 ) => {
107
107
if ( ab . name === '*' ) {
108
108
return true
109
109
}
@@ -118,7 +118,7 @@ export const useAuthStore = defineStore("auth", {
118
118
119
119
hasAllAbilities ( abilities ) {
120
120
let isAvailable = true
121
- if ( this . user . hasOwnProperty ( 'abilities' ) ) {
121
+ if ( this . user && this . user . hasOwnProperty ( 'abilities' ) ) {
122
122
this . user . abilities . filter ( ( ab ) => {
123
123
let hasContain = ! ! abilities . find ( ( p ) => {
124
124
return ab . name === p
You can’t perform that action at this time.
0 commit comments