authenticate($request, $guards) === 'authentication_error') { return response()->json(['error' => 'Unauthorized']); } return $next($request); } protected function authenticate($request, array $guards) { if (empty($guards)) { $guards = [null]; } foreach ($guards as $guard) { if ($this->auth->guard($guard)->check()) { return $this->auth->shouldUse($guard); } } return 'authentication_error'; } /** * Get the path the user should be redirected to when they are not authenticated. * * @param \Illuminate\Http\Request $request * @ string */ protected function redirectTo($request) { if (! $request->expectsJson()) { return route('login'); } } }