]> BookStack Code Mirror - bookstack/blob - app/Http/Middleware/Authenticate.php
Merge branch 'footer-links' of git://github.com/james-geiger/BookStack into james...
[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     use ChecksForEmailConfirmation;
11
12     /**
13      * Handle an incoming request.
14      */
15     public function handle(Request $request, Closure $next)
16     {
17         if ($this->awaitingEmailConfirmation()) {
18             return $this->emailConfirmationErrorResponse($request);
19         }
20
21         if (!hasAppAccess()) {
22             if ($request->ajax()) {
23                 return response('Unauthorized.', 401);
24             } else {
25                 return redirect()->guest(url('/login'));
26             }
27         }
28
29         return $next($request);
30     }
31
32     /**
33      * Provide an error response for when the current user's email is not confirmed
34      * in a system which requires it.
35      */
36     protected function emailConfirmationErrorResponse(Request $request)
37     {
38         if ($request->wantsJson()) {
39             return response()->json([
40                 'error' => [
41                     'code' => 401,
42                     'message' => trans('errors.email_confirmation_awaiting')
43                 ]
44             ], 401);
45         }
46
47         if (session()->get('sent-email-confirmation') === true) {
48             return redirect('/register/confirm');
49         }
50
51         return redirect('/register/confirm/awaiting');
52     }
53 }