I am using a third party authentication mechanism to authenticate users.

On successful authentication, I am able to redirect the users to the front page using the RedirectResponse class. If logged-in users visit the URL, they get an access denied error.

I have built a custom module and the route for the login is /login. I want /user/login to function normally as it did initially. Logged-in users who reach /login must be redirected to the front page, instead of getting an access denied.

I want to achieve this without touching the .htacess file.

How can I achieve it?

  • 1
    You’ve left out some key pieces of information that make this impossible to answer - what url is this supposed to happen on, what is that url provided by, and what are you currently using to deny access to it for authenticated users? – Clive Nov 3 '18 at 12:01
  • @Clive i have updated my question with the URL.I couldn't figure out the answer' deny access for authenticated users.' – Jignesh Rawal Nov 4 '18 at 8:32
  • @Clive thank you for your suggestion to check where access is denied for authenticated user.I changed the routing permission from _user_logged_in to _permission 'access content' now i can try setting the redirection in constructor. – Jignesh Rawal Nov 4 '18 at 9:37

You can do it with hook_user_login

function redirect_user_login($account){
    $url = "/";
    $response = new Symfony\Component\HttpFoundation\RedirectResponse($url);
  • An implementation of hook_user_login() should never redirect users, since it is stopping other implementations of the same hook. – kiamlaluno Nov 4 '18 at 15:18
  • Also, the OP said he has a custom module that implements a custom route. The code shown on this answer would eventually redirect users who just logged in, whatever page they are at. – kiamlaluno Dec 17 '18 at 8:07

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.