Skip to main content

Redirect Users After Login to the Page They Were Viewing in Drupal 8

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.

More about...

Drupal and Drupal Planet

Comments

2016 November 29
Dominique

Permalink

hook_user_login

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

2016 November 30
David Valdez

Permalink

Good to know

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.

2016 November 30
David Valdez

Permalink

I considered it.

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.

2016 November 30
Dominique

Permalink

Was not aware of the issue /

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

The content of this field is kept private and will not be shown publicly.

Markdown

The comment language code.
CAPTCHA Please help us focus on people and not spambots by answering this question.