@@ -38,6 +38,33 @@ class Auth
38
38
*/
39
39
protected $ errorsArray = [];
40
40
41
+ public function __construct ()
42
+ {
43
+ $ this ->middleware ('auth.required ' , function () {
44
+ response ()->redirect ('/auth/login ' , 401 );
45
+ });
46
+
47
+ $ this ->middleware ('auth.guest ' , function () {
48
+ response ()->json ('/dashboard ' , 401 );
49
+ });
50
+
51
+ $ this ->middleware ('is ' , function ($ role ) {
52
+ \Leaf \Exception \General::default404 ();
53
+ });
54
+
55
+ $ this ->middleware ('isNot ' , function () {
56
+ \Leaf \Exception \General::default404 ();
57
+ });
58
+
59
+ $ this ->middleware ('can ' , function () {
60
+ \Leaf \Exception \General::default404 ();
61
+ });
62
+
63
+ $ this ->middleware ('cannot ' , function () {
64
+ \Leaf \Exception \General::default404 ();
65
+ });
66
+ }
67
+
41
68
/**
42
69
* Connect leaf auth to the database
43
70
* @param array $dbConfig Configuration for leaf db connection
@@ -594,6 +621,7 @@ public function middleware(string $middleware, callable $callback)
594
621
return app ()->registerMiddleware ('auth.required ' , function () use ($ callback ) {
595
622
if (!$ this ->user ()) {
596
623
$ callback ();
624
+ exit ;
597
625
}
598
626
});
599
627
}
@@ -602,6 +630,7 @@ public function middleware(string $middleware, callable $callback)
602
630
return app ()->registerMiddleware ('auth.guest ' , function () use ($ callback ) {
603
631
if ($ this ->user ()) {
604
632
$ callback ();
633
+ exit ;
605
634
}
606
635
607
636
auth ()->clearErrors ();
@@ -611,31 +640,35 @@ public function middleware(string $middleware, callable $callback)
611
640
if ($ middleware === 'is ' ) {
612
641
return app ()->registerMiddleware ('is ' , function ($ role ) use ($ callback ) {
613
642
if ($ this ->user ()?->isNot($ role )) {
614
- $ callback ();
643
+ $ callback ($ role );
644
+ exit ;
615
645
}
616
646
});
617
647
}
618
648
619
649
if ($ middleware === 'isNot ' ) {
620
650
return app ()->registerMiddleware ('isNot ' , function ($ role ) use ($ callback ) {
621
651
if ($ this ->user ()?->is($ role )) {
622
- $ callback ();
652
+ $ callback ($ role );
653
+ exit ;
623
654
}
624
655
});
625
656
}
626
657
627
658
if ($ middleware === 'can ' ) {
628
659
return app ()->registerMiddleware ('can ' , function ($ role ) use ($ callback ) {
629
- if ($ this ->user ()?->can($ role )) {
630
- $ callback ();
660
+ if ($ this ->user ()?->cannot($ role )) {
661
+ $ callback ($ role );
662
+ exit ;
631
663
}
632
664
});
633
665
}
634
666
635
667
if ($ middleware === 'cannot ' ) {
636
668
return app ()->registerMiddleware ('cannot ' , function ($ role ) use ($ callback ) {
637
- if ($ this ->user ()?->cannot($ role )) {
638
- $ callback ();
669
+ if ($ this ->user ()?->can($ role )) {
670
+ $ callback ($ role );
671
+ exit ;
639
672
}
640
673
});
641
674
}
0 commit comments