0

I'm looking to redirect or prevent access for authenticated users to the /user page. The reason being that it serves no purpose being there so would like to either remove or redirect it.

The best I've managed so far is to redirect the user on login so they don't get dropped on the /user page straight after logging in which works great but I'd like to prevent all access to it. The code I've used for the redirect is below:

function mymodule_user_login($account) {
  // Redirect users on login
  $link = Link::createFromRoute(
    "Admin",
    "admin.home"
  )->getUrl()->toString();

  $homeResponse = new RedirectResponse("{$link}");
  $homeResponse->send();

  return;
}

Just a note for anyone potentially using the above code - it only works in a .module file, not a .theme file which took me a while to work out.

  • 1
    You can use a redirect response, but never send a response in custom code. You get two responses then, one you've sent and one Drupal builds in the background, which the browser discards because it already executed the redirect, but Drupal thinks it is the real one and stores it in the cache. Set the redirect response in an event subscriber instead. A request subscriber with a priority of 28 would be a good starting point, see drupal.stackexchange.com/questions/201273/… – 4k4 Sep 11 '18 at 6:34
  • @4k4 Thanks for that, certainly given me a good starting point. – Neil Nand Sep 11 '18 at 9:13
0

You can probably use access_filter to prevent access to a specific URL.

It can setup filters that will allow you to deny access to specific URL based on IP (or all IPs) and specify where to redirect anyone who is trying to access that URL.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.