Answers.Body: <p>You could set the template they get redirected to after successfully logged in, then in there, redirect them based on the user group they belong to.</p>
  
  <pre class="lang-twig prettyprint-override"><code>{% if currentUser.isInGroup('userGroupA') %}
      {% redirect 'path/a' %}
  {% elseif currentUser.isInGroup('userGroupB') %}
      {% redirect 'path/b' %}
  {% else %}
      {% redirect 'path/c' %}
  {% endif %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-02-27T18:30:51.593Z
Answers.Text: That's an interesting approach! I like the idea of adding the URL to a custom field of the member group, since it would be useful for other user-specific sections / data, as well. I'm hoping though that someone can describe an example of using `postLoginRedirect` dynamically!
Answers.UserDisplayName: Nathan D Huening
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-02-27T18:54:01.137Z
Answers.Text: I can't think of a way for it to be dynamic unless there is some event that is called to change the value being passed to postLoginRedirect. And I don't if its possible to add some session logic into the general config file itself.
Answers.UserDisplayName: Brad
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-02-27T18:55:57.630Z
Answers.Text: Brandon's answer is good, my answer would work as well if you don't want to build a plugin.
Answers.UserDisplayName: Brad
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-02-27T19:00:11.227Z
Answers.Text: This is a great lo-fi solution. Just updated it to make it actually work, for anyone that wants to go this route instead.
Answers.UserDisplayName: Brandon Kelly
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-03-01T15:48:17.060Z
Answers.Text: Agreed! Happy to throw together a plugin if necessary but nice to know there's a quick-and-dirty solution (which is all I need most of the time). Thanks Brad
Answers.UserDisplayName: Nathan D Huening
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-02-27T18:23:55.363Z
Answers.LastActivityDate: 2017-02-27T18:58:54.207Z
Answers.LastEditDate: 2017-02-27T18:58:54.207Z
Answers.UserDisplayName: Brad
Answers.Body: <p>To set the setting dynamically, you’ll need to create a new plugin, which just has an <code>init()</code> method with this:</p>
  
  <pre class="lang-php prettyprint-override"><code>public function init()
  {
      // Is this a login request?
      if (
          craft()-&gt;request-&gt;getActionSegments() === array('users', 'login') &amp;&amp;
          ($loginName = craft()-&gt;request-&gt;getPost('loginName')) !== null &amp;&amp;
          ($user = craft()-&gt;users-&gt;getUserByUsernameOrEmail($loginName)) !== null
      )
      {
          if ($user-&gt;isInGroup('userGroupA'))
          {
              craft()-&gt;config-&gt;set('postLoginRedirect', 'path/a');
          }
          else if ($user-&gt;isInGroup('userGroupB'))
          {
              craft()-&gt;config-&gt;set('postLoginRedirect', 'path/b');
          }
      }
  }
  </code></pre>
  
  <p>If none of the conditions match, then Craft will stick with whatever’s in your <code>craft/config/general.php</code> file.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-03-01T15:48:39.850Z
Answers.Text: Awesome, thanks Brandon.
Answers.UserDisplayName: Nathan D Huening
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-02-27T18:51:12.683Z
Answers.LastActivityDate: 2017-02-27T18:51:12.683Z
Answers.UserDisplayName: Brandon Kelly
Body: <p>I'd like to control where a user gets re-directed after login based on the user group. That is, userGroupA logs in and is sent to /templateA/_entry, while userGroupB logs in and is sent to /templateB/_entry. </p>
  
  <p>I understand that <code>postLoginRedirect</code> can be used dynamically. Are there any examples of this?</p>
  
  <p>Thanks!</p>
  
Tags: <users>
Title: Redirecting after login based on user group
ThreadText: # Redirecting after login based on user group
  
  (Asked by: username_0 on 2017-02-27)
  
  I'd like to control where a user gets re-directed after login based on the user group. That is, userGroupA logs in and is sent to /templateA/\_entry, while userGroupB logs in and is sent to /templateB/\_entry.
  
  I understand that `postLoginRedirect` can be used dynamically. Are there any examples of this?
  
  Thanks!
  
  **Tags:** users
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_2 on 2017-02-27. Score: 7 | Accepted answer
  
  To set the setting dynamically, you’ll need to create a new plugin, which just has an `init()` method with this:
  
  ```php
  public function init()
  {
      // Is this a login request?
      if (
          craft()->request->getActionSegments() === array('users', 'login') &&
          ($loginName = craft()->request->getPost('loginName')) !== null &&
          ($user = craft()->users->getUserByUsernameOrEmail($loginName)) !== null
      )
      {
          if ($user->isInGroup('userGroupA'))
          {
              craft()->config->set('postLoginRedirect', 'path/a');
          }
          else if ($user->isInGroup('userGroupB'))
          {
              craft()->config->set('postLoginRedirect', 'path/b');
          }
      }
  }
  
  ```
  
  If none of the conditions match, then Craft will stick with whatever’s in your `craft/config/general.php` file.
  
  > **Comments:**
  > **username_0**: Awesome, thanks username_2.  
  
  ---
  
  ### Answer by username_1 on 2017-02-27. Score: 2
  
  You could set the template they get redirected to after successfully logged in, then in there, redirect them based on the user group they belong to.
  
  ```twig
  {% if currentUser.isInGroup('userGroupA') %}
      {% redirect 'path/a' %}
  {% elseif currentUser.isInGroup('userGroupB') %}
      {% redirect 'path/b' %}
  {% else %}
      {% redirect 'path/c' %}
  {% endif %}
  
  ```
  
  > **Comments:**
  > **username_0**: That's an interesting approach! I like the idea of adding the URL to a custom field of the member group, since it would be useful for other user-specific sections / data, as well. I'm hoping though that someone can describe an example of using `postLoginRedirect` dynamically!  
  > **username_1**: I can't think of a way for it to be dynamic unless there is some event that is called to change the value being passed to postLoginRedirect. And I don't if its possible to add some session logic into the general config file itself.  
  > **username_1**: username_2's answer is good, my answer would work as well if you don't want to build a plugin.  
  > **username_2**: This is a great lo-fi solution. Just updated it to make it actually work, for anyone that wants to go this route instead.  
  > **username_0**: Agreed! Happy to throw together a plugin if necessary but nice to know there's a quick-and-dirty solution (which is all I need most of the time). Thanks username_1  
  
  ---
  
  