]> BookStack Code Mirror - bookstack/blob - app/Http/Middleware/Authenticate.php
Added basic system tests for markdown editor, Added extra test helpers
[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 use BookStack\Exceptions\UserRegistrationException;
8 use Setting;
9
10 class Authenticate
11 {
12     /**
13      * The Guard implementation.
14      *
15      * @var Guard
16      */
17     protected $auth;
18
19     /**
20      * Create a new filter instance.
21      *
22      * @param  Guard $auth
23      */
24     public function __construct(Guard $auth)
25     {
26         $this->auth = $auth;
27     }
28
29     /**
30      * Handle an incoming request.
31      *
32      * @param  \Illuminate\Http\Request  $request
33      * @param  \Closure  $next
34      * @return mixed
35      */
36     public function handle($request, Closure $next)
37     {
38         if(auth()->check() && auth()->user()->email_confirmed == false) {
39             return redirect()->guest('/register/confirm/awaiting');
40         }
41
42         if ($this->auth->guest() && !setting('app-public')) {
43             if ($request->ajax()) {
44                 return response('Unauthorized.', 401);
45             } else {
46                 return redirect()->guest('/login');
47             }
48         }
49
50         return $next($request);
51     }
52 }