Authenticate::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
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