]> BookStack Code Mirror - bookstack/blob - app/Http/Middleware/Authenticate.php
Got standard form-based registration working
[bookstack] / app / Http / Middleware / Authenticate.php
1 <?php
2
3 namespace Oxbow\Http\Middleware;
4
5 use Closure;
6 use Illuminate\Contracts\Auth\Guard;
7 use Oxbow\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         if ($this->auth->guest() && !Setting::get('app-public')) {
42             if ($request->ajax()) {
43                 return response('Unauthorized.', 401);
44             } else {
45                 return redirect()->guest('/login');
46             }
47         }
48
49         return $next($request);
50     }
51 }