Authenticate   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 41
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A handle() 0 13 2
1
<?php
2
3
namespace App\Http\Middleware;
4
5
use App\Constants\TranslationCode;
6
use Closure;
7
use Illuminate\Contracts\Auth\Factory as Auth;
8
use Illuminate\Http\JsonResponse;
9
10
/**
11
 * Class Authenticate
12
 *
13
 * @package App\Http\Middleware
14
 */
15
class Authenticate
16
{
17
    /**
18
     * The authentication guard factory instance.
19
     *
20
     * @var Auth
21
     */
22
    protected $auth;
23
24
    /**
25
     * Authenticate constructor.
26
     *
27
     * @param  Auth  $auth
28
     */
29
    public function __construct(Auth $auth)
30
    {
31
        $this->auth = $auth;
32
    }
33
34
    /**
35
     * Handle an incoming request.
36
     *
37
     * @param $request
38
     * @param  Closure  $next
39
     * @param $guard
40
     *
41
     * @return JsonResponse|mixed
42
     */
43
    public function handle($request, Closure $next, $guard = null)
44
    {
45
        if ($this->auth->guard($guard)->guest()) {
46
            $response = [
47
                'isError'       => true,
48
                'userFault'     => true,
49
                'errorMessages' => ['authorization' => TranslationCode::ERROR_UNAUTHORIZED]
50
            ];
51
52
            return response()->json($response);
53
        }
54
55
        return $next($request);
56
    }
57
}
58