Redirect users after login to the page they were viewing in Drupal 8

Submitted by on

Have you ever been asked to log into a website while you are viewing a page? And after doing so you get redirected to some page other than the one you were reading? This is an obvious and rather common usability problem. When this happens people lose track of what they were doing and some might not even bother to go back. Let's find out how to solve this in Drupal 8.

In a recent project a client wisely requested exactly that: whenever a user logs into the site, redirect them to the page they were before clicking the login link. This seemed like a very common request so we looked for a contrib module that provided the functionality. Login Destination used to do it in Drupal 7. Sadly the Drupal 8 version of this module does not provide the functionality yet.

Other modules, and some combinations of them, were tested without success. Therefore, we built Login Return Page. It a very small module that just does one thing and it does it well: it appends destination=/current/page to all the links pointing to /user/login effectively redirecting users to the page they were viewing before login. The project is waiting to be approved before promoting it to full project.

Have you had a similar need? Are there other things you are requested to do after login? Please share them in the comments.

UPDATE: It seems to this is a regression from Drupal 7 and there is an issue that would fix it. Thanks to Wim Leers for letting me know about it.

Comments

Submitted by Dominique on Tue, 11/29/2016 - 19:49

Hi,
interesting approach via javascript solution.
Ism't it possible to do it with hook_user_login() Implementation as well?

Submitted by on Wed, 11/30/2016 - 10:01

Hi! Dominique.

I considered doing it using the hook_user_login() but having the cache activated in the site I wasn't sure if this was going to work, so I prefer doing it in the JS side which will work even if the is cache is activated.

But reading Wim's comment it seems that this will be fixed in core soon.

Thanks for your comment.

Submitted by Wim Leers on Wed, 11/30/2016 - 02:27

We don't need a contrib module for this. We need to fix a regression in Drupal 8 core: https://www.drupal.org/node/2582797.

Unlike what this blog post claims, Drupal 7 did this out of the box. https://www.drupal.org/project/login_destination was only necessary for more advanced use cases.

Submitted by on Wed, 11/30/2016 - 09:59

Hi Wim.

I didn't know about this as a regression. I will check the Issue to see if I can help to fix it.

Thanks for the clarification.

Submitted by Dominique on Wed, 11/30/2016 - 19:25

Was not aware of the issue / regression as well.
I did implement a redirect script in a custom module as this functionality seems a basic one when talking about user experience.
However, when I look at the issue itself, I wonder why it needs so much of time and discussion in order not to be solved after 1 year. As a
neophyte I thought this can be quickly solved.
I am 200% supporter of Drupal, but sometimes, I am lost into all those "processes" and I feel there is something I am not aware of...

Add new comment