@@ -38,6 +38,33 @@ class Auth
3838 */
3939 protected $ errorsArray = [];
4040
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+
4168 /**
4269 * Connect leaf auth to the database
4370 * @param array $dbConfig Configuration for leaf db connection
@@ -594,6 +621,7 @@ public function middleware(string $middleware, callable $callback)
594621 return app ()->registerMiddleware ('auth.required ' , function () use ($ callback ) {
595622 if (!$ this ->user ()) {
596623 $ callback ();
624+ exit ;
597625 }
598626 });
599627 }
@@ -602,6 +630,7 @@ public function middleware(string $middleware, callable $callback)
602630 return app ()->registerMiddleware ('auth.guest ' , function () use ($ callback ) {
603631 if ($ this ->user ()) {
604632 $ callback ();
633+ exit ;
605634 }
606635
607636 auth ()->clearErrors ();
@@ -611,31 +640,35 @@ public function middleware(string $middleware, callable $callback)
611640 if ($ middleware === 'is ' ) {
612641 return app ()->registerMiddleware ('is ' , function ($ role ) use ($ callback ) {
613642 if ($ this ->user ()?->isNot($ role )) {
614- $ callback ();
643+ $ callback ($ role );
644+ exit ;
615645 }
616646 });
617647 }
618648
619649 if ($ middleware === 'isNot ' ) {
620650 return app ()->registerMiddleware ('isNot ' , function ($ role ) use ($ callback ) {
621651 if ($ this ->user ()?->is($ role )) {
622- $ callback ();
652+ $ callback ($ role );
653+ exit ;
623654 }
624655 });
625656 }
626657
627658 if ($ middleware === 'can ' ) {
628659 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 ;
631663 }
632664 });
633665 }
634666
635667 if ($ middleware === 'cannot ' ) {
636668 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 ;
639672 }
640673 });
641674 }
0 commit comments