1

Quote from the {% requireLogin %} docs:

This tag will ensure that the user is logged in. If they aren’t, they will be redirected to the Login page, and returned to the original page after successfully logging in.

Quote from the Login Form example in the docs:

craft.session.returnUrl is set to the original URL that included the {% requireLogin %} tag that initiated the redirect to this login form. By default, users will be redirected based on your postLoginRedirect config setting value after logging in.

That seems to be contradictory - the first sentence saying the return path will be the page that contained the requireLogin tag and the second saying the return path is defined by a config setting.

I want to be able to have the redirect go to the page that calls the login (I have a few of them), but do not see a way of doing that. Using a return parameter in the login form does not work for obvious reasons.

Am I missing something here? Any wisdom greatly appreciated!

1

By default Craft checks the postLoginRedirect setting in your config. Depending on this value the user is going to be redirected to certain place. But if there is a value in the user session that's set when the requireLogin function is called Yii2 will use this value instead. So the workflow is the following

  1. Check for a session value, if there is one, redirect the user
  2. Check for postLoginRedirect value, if there is one, redirect the user
  3. Redirect to homepage

Using a return parameter in the login form does not work for obvious reasons.

Could you explain those obvious reasons? Because I always use the returnUrl from UsersController when I create a login via ajax.

You don't have to do anything actually. Just include the Twig token and Yii2 will handle it

  • Thanks, Robin. I have a number of different pages that use requireLogin, so the redirect needs to be set dynamically, which is why I can't hard code it into the login form set it in the postLoginRedirect in my config file. What Twig token are you referring to? – Roi Agneta Feb 26 at 23:06
  • The {% requireLogin %} token will exactly do that for you. When a user enters a page with that token and is a guest (not logged in) the current route is stored in the session and the user will be redirected to your login. After a successful login yii2 will grab the session value and redirect the user back to the route where he was before / to the site he was going to see before he/she logged in – Robin Schambach Feb 26 at 23:08
  • Yes, that is the way it is supposed to work, but it doesn't, which is why I posted this question. The return is the home page, no matter what. Note that craft/src/GeneralConfig has this: public $postLoginRedirect = ' '; On lastest version of Craft 3 – Roi Agneta Feb 27 at 0:16
  • You may want to submit a bug report or something. I use the tag in many projects and it always works, even in the latest version of Craft. Do you have some custom code or a plugin that might interfere? Also: do you submit the form normally or via Ajax? Do you use a custom Controller or users/login – Robin Schambach Feb 27 at 7:40

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.