Completed
Push — master ( 02f92d...9c9507 )
by Sebastian
15:38
created

Authenticate::handleUnauthorizedRequest()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 16
rs 9.2
cc 4
eloc 8
nc 4
nop 1
1
<?php
2
3
namespace App\Http\Middleware;
4
5
use App\Services\Navigation\Section;
6
use Closure;
7
use Exception;
8
9
class Authenticate
10
{
11
    public function handle($request, Closure $next)
12
    {
13
        if (! current_user()) {
14
            return $this->handleUnauthorizedRequest($request);
15
        }
16
17
        current_user()->registerLastActivity()->save();
18
19
        return $next($request);
20
    }
21
22
    protected function handleUnauthorizedRequest($request)
23
    {
24
        if ($request->ajax()) {
25
            return response('Unauthorized.', 401);
26
        }
27
28
        if (app(Section::class)->isFront()) {
29
            return redirect()->guest(action('Front\AuthController@getLogin'));
30
        }
31
32
        if (app(Section::class)->isBack()) {
33
            return redirect()->guest(action('Back\AuthController@getLogin'));
34
        }
35
36
        throw new Exception('Invalid auth guard specified');
37
    }
38
}
39