dispatcher = $dispatcher; $this->uriBuilder = $uriBuilder; $this->listenerProvider = $listenerProvider; } /** * Handles a backend request, after finishing running middlewares * Dispatch the request to the appropriate controller through the * Backend Dispatcher which resolves the routing */ public function handle(ServerRequestInterface $request): ResponseInterface { // Make sure all FAL resources have absolute URL paths $this->listenerProvider->addListener( GeneratePublicUrlForResourceEvent::class, PublicUrlPrefixer::class, 'prefixWithSitePath' ); /** @var Route $route */ $route = $request->getAttribute('route'); $isAjaxCall = (bool)($route->getOption('ajax') ?? false); // b/w compat $GLOBALS['TYPO3_REQUEST'] = $request; try { // Check if the router has the available route and dispatch. return $this->dispatcher->dispatch($request); } catch (MissingRequestTokenException $e) { if ($isAjaxCall) { return new Response(statusCode: 401); } // When token was missing, then redirect to login, but keep the current route as redirect after login $loginUrl = $this->uriBuilder->buildUriWithRedirect( 'login', [], RouteRedirect::createFromRoute($request->getAttribute('route'), $request->getQueryParams()) ); return new RedirectResponse($loginUrl); } catch (InvalidRequestTokenException $e) { if ($isAjaxCall) { return new Response(statusCode: 401); } // When token was invalid, then redirect to login $loginForm = $this->uriBuilder->buildUriFromRoute('login'); return new RedirectResponse($loginForm); } } }