<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>

CC BY-SA 3.0
2017-02-27T18:30:51.593Z
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!
Nathan D Huening
CC BY-SA 3.0
2017-02-27T18:54:01.137Z
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.
Brad
CC BY-SA 3.0
2017-02-27T18:55:57.630Z
Brandon's answer is good, my answer would work as well if you don't want to build a plugin.
Brad
CC BY-SA 3.0
2017-02-27T19:00:11.227Z
This is a great lo-fi solution. Just updated it to make it actually work, for anyone that wants to go this route instead.
Brandon Kelly
CC BY-SA 3.0
2017-03-01T15:48:17.060Z
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
Nathan D Huening
CC BY-SA 3.0
2017-02-27T18:23:55.363Z
2017-02-27T18:58:54.207Z
2017-02-27T18:58:54.207Z
Brad
<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>

CC BY-SA 3.0
2017-03-01T15:48:39.850Z
Awesome, thanks Brandon.
Nathan D Huening
CC BY-SA 3.0
2017-02-27T18:51:12.683Z
2017-02-27T18:51:12.683Z
Brandon Kelly
<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>

<users>
Redirecting after login based on user group
# 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:
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 Brandon.  

---

### 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.
{% 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**: Brandon'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 Brad  

---

