]> BookStack Code Mirror - bookstack/blob - app/Http/Middleware/ConfirmEmails.php
Linked new API token system into middleware
[bookstack] / app / Http / Middleware / ConfirmEmails.php
1 <?php
2
3 namespace BookStack\Http\Middleware;
4
5 use BookStack\Http\Request;
6 use Closure;
7 use Illuminate\Contracts\Auth\Guard;
8
9 /**
10  * Confirms the current user's email address.
11  * Must come after any middleware that may log users in.
12  */
13 class ConfirmEmails
14 {
15     /**
16      * The Guard implementation.
17      */
18     protected $auth;
19
20     /**
21      * Create a new ConfirmEmails instance.
22      */
23     public function __construct(Guard $auth)
24     {
25         $this->auth = $auth;
26     }
27
28     /**
29      * Handle an incoming request.
30      */
31     public function handle(Request $request, Closure $next)
32     {
33         if ($this->auth->check()) {
34             $requireConfirmation = (setting('registration-confirmation') || setting('registration-restrict'));
35             if ($requireConfirmation && !$this->auth->user()->email_confirmed) {
36                 return $this->errorResponse($request);
37             }
38         }
39
40         return $next($request);
41     }
42
43     /**
44      * Provide an error response for when the current user's email is not confirmed
45      * in a system which requires it.
46      */
47     protected function errorResponse(Request $request)
48     {
49         if ($request->wantsJson()) {
50             return response()->json([
51                 'error' => [
52                     'code' => 401,
53                     'message' => trans('errors.email_confirmation_awaiting')
54                 ]
55             ], 401);
56         }
57
58         return redirect('/register/confirm/awaiting');
59     }
60 }