Answers.Body: <p>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 <code>requireLogin</code> function is called Yii2 will use this value instead. So the workflow is the following</p>
  
  <ol>
  <li>Check for a session value, if there is one, redirect the user</li>
  <li>Check for <code>postLoginRedirect</code> value, if there is one, redirect the user</li>
  <li>Redirect to homepage</li>
  </ol>
  
  <blockquote>
    <p>Using a return parameter in the login form does not work for obvious reasons.</p>
  </blockquote>
  
  <p>Could you explain those obvious reasons? Because I always use the <code>returnUrl</code> from <code>UsersController</code> when I create a login via ajax.</p>
  
  <p>You don't have to do anything actually. Just include the Twig token and Yii2 will handle it</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-02-26T23:06:14.620Z
Answers.Text: 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?
Answers.UserDisplayName: Roi Agneta
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-02-26T23:08:52.530Z
Answers.Text: 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
Answers.UserDisplayName: Robin Schambach
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-02-27T00:16:53.357Z
Answers.Text: 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
Answers.UserDisplayName: Roi Agneta
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-02-27T07:40:47.300Z
Answers.Text: 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`
Answers.UserDisplayName: Robin Schambach
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-02-26T20:14:50.207Z
Answers.LastActivityDate: 2019-02-26T20:21:13.207Z
Answers.LastEditDate: 2019-02-26T20:21:13.207Z
Answers.UserDisplayName: Robin Schambach
Body: <p>Quote from the {% requireLogin %} docs:</p>
  
  <blockquote>
    <p>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.</p>
  </blockquote>
  
  <p>Quote from the Login Form example in the docs:</p>
  
  <blockquote>
    <p>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. </p>
  </blockquote>
  
  <p>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.</p>
  
  <p>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. </p>
  
  <p>Am I missing something here? Any wisdom greatly appreciated!</p>
  
Tags: <login>
Title: Confusion over 'require login' behavior
ThreadText: # Confusion over 'require login' behavior
  
  (Asked by: username_0 on 2019-02-26)
  
  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!
  
  **Tags:** login
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2019-02-26. Score: 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
  
  > **Comments:**
  > **username_0**: Thanks, username_1. 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?  
  > **username_1**: 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  
  > **username_0**: 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  
  > **username_1**: 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`  
  
  ---
  
  