11import { createResource } from "frappe-ui" ;
2- import { createRouter , createWebHistory } from "vue-router" ;
2+ import { NavigationGuardNext , RouteLocationNormalized , createRouter , createWebHistory } from "vue-router" ;
33
4- // Hack, TODO: Check authentication
54let hasPermission : null | boolean = null ;
65
7- function validatePermission ( next : CallableFunction ) {
6+ function validatePermission ( next : NavigationGuardNext ) {
87 if ( hasPermission ) {
98 next ( ) ;
109 } else {
@@ -13,7 +12,11 @@ function validatePermission(next: CallableFunction) {
1312 }
1413}
1514
16- const validateVisit = function ( to : string , from : string , next : CallableFunction ) {
15+ const validateVisit = function (
16+ to : RouteLocationNormalized ,
17+ from : RouteLocationNormalized ,
18+ next : NavigationGuardNext
19+ ) {
1720 if ( document . cookie . includes ( "user_id" ) && ! document . cookie . includes ( "user_id=Guest" ) ) {
1821 if ( hasPermission === null ) {
1922 createResource ( {
@@ -25,15 +28,19 @@ const validateVisit = function (to: string, from: string, next: CallableFunction
2528 docname : null ,
2629 perm_type : "write" ,
2730 } )
28- . then ( ( res : any ) => {
29- hasPermission = res . has_permission as boolean ;
31+ . then ( ( res : { has_permission : boolean } ) => {
32+ hasPermission = res . has_permission ;
33+ validatePermission ( next ) ;
34+ } )
35+ . catch ( ( ) => {
36+ hasPermission = false ;
3037 validatePermission ( next ) ;
3138 } ) ;
3239 } else {
3340 validatePermission ( next ) ;
3441 }
3542 } else {
36- window . location . href = "/login" ;
43+ validatePermission ( next ) ;
3744 }
3845} ;
3946
0 commit comments