3 namespace BookStack\Http\Middleware;
5 use BookStack\Exceptions\ApiAuthException;
7 use Illuminate\Http\Request;
11 use ChecksForEmailConfirmation;
14 * Handle an incoming request.
16 public function handle(Request $request, Closure $next)
18 // Return if the user is already found to be signed in via session-based auth.
19 // This is to make it easy to browser the API via browser after just logging into the system.
21 if ($this->awaitingEmailConfirmation()) {
22 return $this->emailConfirmationErrorResponse($request);
24 return $next($request);
27 // Set our api guard to be the default for this request lifecycle.
28 auth()->shouldUse('api');
30 // Validate the token and it's users API access
32 auth()->authenticate();
33 } catch (ApiAuthException $exception) {
34 return $this->unauthorisedResponse($exception->getMessage(), $exception->getCode());
37 if ($this->awaitingEmailConfirmation()) {
38 return $this->emailConfirmationErrorResponse($request);
41 return $next($request);
45 * Provide a standard API unauthorised response.
47 protected function unauthorisedResponse(string $message, int $code)
49 return response()->json([
52 'message' => $message,