]> BookStack Code Mirror - bookstack/blob - app/Http/Middleware/Authenticate.php
Merge pull request #3 from BookStackApp/master
[bookstack] / app / Http / Middleware / Authenticate.php
1 <?php
2
3 namespace BookStack\Http\Middleware;
4
5 use Closure;
6 use Illuminate\Contracts\Auth\Guard;
7
8 class Authenticate
9 {
10     /**
11      * The Guard implementation.
12      * @var Guard
13      */
14     protected $auth;
15
16     /**
17      * Create a new filter instance.
18      * @param  Guard $auth
19      */
20     public function __construct(Guard $auth)
21     {
22         $this->auth = $auth;
23     }
24
25     /**
26      * Handle an incoming request.
27      * @param  \Illuminate\Http\Request  $request
28      * @param  \Closure  $next
29      * @return mixed
30      */
31     public function handle($request, Closure $next)
32     {
33         if ($this->auth->check() && setting('registration-confirmation') && !$this->auth->user()->email_confirmed) {
34             return redirect(baseUrl('/register/confirm/awaiting'));
35         }
36
37         if ($this->auth->guest() && !setting('app-public')) {
38             if ($request->ajax()) {
39                 return response('Unauthorized.', 401);
40             } else {
41                 return redirect()->guest(baseUrl('/login'));
42             }
43         }
44
45         return $next($request);
46     }
47 }