]> BookStack Code Mirror - bookstack/blob - app/Http/Middleware/Authenticate.php
b9b5545f2a541a302e6a60b81985009f4dc62723
[bookstack] / app / Http / Middleware / Authenticate.php
1 <?php
2
3 namespace BookStack\Http\Middleware;
4
5 use Closure;
6 use Illuminate\Http\Request;
7
8 class Authenticate
9 {
10     /**
11      * Handle an incoming request.
12      */
13     public function handle(Request $request, Closure $next)
14     {
15         if (!hasAppAccess()) {
16             if ($request->ajax()) {
17                 return response('Unauthorized.', 401);
18             }
19             return redirect()->guest(url('/login'));
20         }
21
22         return $next($request);
23     }
24
25     /**
26      * Provide an error response for when the current user's email is not confirmed
27      * in a system which requires it.
28      */
29     protected function emailConfirmationErrorResponse(Request $request)
30     {
31         if ($request->wantsJson()) {
32             return response()->json([
33                 'error' => [
34                     'code'    => 401,
35                     'message' => trans('errors.email_confirmation_awaiting'),
36                 ],
37             ], 401);
38         }
39
40         if (session()->get('sent-email-confirmation') === true) {
41             return redirect('/register/confirm');
42         }
43
44         return redirect('/register/confirm/awaiting');
45     }
46 }