Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
90.91% |
10 / 11 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| Authenticate | |
90.91% |
10 / 11 |
|
50.00% |
1 / 2 |
6.03 | |
0.00% |
0 / 1 |
| redirectTo | |
80.00% |
4 / 5 |
|
0.00% |
0 / 1 |
4.13 | |||
| guards | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Http\Middleware; |
| 4 | |
| 5 | use Illuminate\Auth\Middleware\Authenticate as Middleware; |
| 6 | use Illuminate\Http\Request; |
| 7 | |
| 8 | class Authenticate extends Middleware |
| 9 | { |
| 10 | /** |
| 11 | * Get the path the user should be redirected to when they are not authenticated. |
| 12 | * |
| 13 | * @param Request $request |
| 14 | * @return string|null |
| 15 | */ |
| 16 | protected function redirectTo($request) |
| 17 | { |
| 18 | if ($request->expectsJson()) { |
| 19 | return null; |
| 20 | } |
| 21 | |
| 22 | // A request hitting an `auth:customer` route (storefront /account/*) |
| 23 | // should land on the customer login, not the admin one. |
| 24 | if (in_array('customer', $this->guards($request) ?: [], true)) { |
| 25 | return route('account.login'); |
| 26 | } |
| 27 | |
| 28 | return route('login'); |
| 29 | } |
| 30 | |
| 31 | /** Guard names this middleware instance was invoked with (auth:customer,auth:web,...). */ |
| 32 | private function guards($request): array |
| 33 | { |
| 34 | return $request->route()?->middleware() |
| 35 | ? collect($request->route()->gatherMiddleware()) |
| 36 | ->filter(fn ($m) => str_starts_with($m, 'auth:')) |
| 37 | ->flatMap(fn ($m) => explode(',', substr($m, 5))) |
| 38 | ->all() |
| 39 | : []; |
| 40 | } |
| 41 | } |