3 namespace BookStack\Http\Middleware;
5 use BookStack\Exceptions\ApiAuthException;
6 use BookStack\Http\Request;
13 * Handle an incoming request.
15 public function handle(Request $request, Closure $next)
17 // Return if the user is already found to be signed in via session-based auth.
18 // This is to make it easy to browser the API via browser after just logging into the system.
20 return $next($request);
23 // Set our api guard to be the default for this request lifecycle.
24 auth()->shouldUse('api');
26 // Validate the token and it's users API access
28 auth()->authenticate();
29 } catch (ApiAuthException $exception) {
30 return $this->unauthorisedResponse($exception->getMessage(), $exception->getCode());
33 return $next($request);
37 * Provide a standard API unauthorised response.
39 protected function unauthorisedResponse(string $message, int $code)
41 return response()->json([
44 'message' => $message,