File tree 2 files changed +47
-0
lines changed
2 files changed +47
-0
lines changed Original file line number Diff line number Diff line change @@ -46,3 +46,19 @@ For clear error responses you need to add the trait in the **App\Exceptions\Hand
46
46
}
47
47
48
48
```
49
+
50
+
51
+ ## Auth Middleware
52
+
53
+ If you wants to set the auth user for every authenticated route then add RestApiMiddleware in your project and apply in on routes.
54
+
55
+ ```
56
+ In kernel.php
57
+
58
+ 'restapi' => ColoredCow\LaravelMobileAPI\RestAPIMiddleware::class,
59
+
60
+ In routes file
61
+
62
+ Route::group(['middleware' => ['auth:api', 'restapi:auth'] ], function () ...
63
+
64
+ ```
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace ColoredCow \LaravelMobileAPI ;
4
+
5
+ use Illuminate \Support \Facades \Auth ;
6
+ use Closure ;
7
+
8
+ class RestAPIMiddleware
9
+ {
10
+ /**
11
+ * Handle an incoming request.
12
+ *
13
+ * @param \Illuminate\Http\Request $request
14
+ * @param \Closure $next
15
+ * @return mixed
16
+ */
17
+ public function handle ($ request , Closure $ next , $ type = '' )
18
+ {
19
+ if ($ type == 'auth ' ) {
20
+ return $ this ->handleAuthRequest ($ request , $ next );
21
+ }
22
+
23
+ return $ next ($ request );
24
+ }
25
+
26
+ private function handleAuthRequest ($ request , $ next ) {
27
+ $ user = $ request ->user ();
28
+ Auth::setUser ($ user );
29
+ return $ next ($ request );
30
+ }
31
+ }
You can’t perform that action at this time.
0 commit comments