<p>This is an alternate solution to what you're trying to do, and only applies if the login is coming and being directed to front end templates, but you could do something similar directly in your twig template, rather than having to use a plugin.</p>

<p>You could put something like...</p>

<pre><code>{% if currentUser and currentUser.isInGroup('sales') %}
{% redirect "new.url" %}
{% endif %}
</code></pre>

<p>...somewhere on the page that logged in users will land on by default.</p>

CC BY-SA 3.0
2017-04-27T15:16:49.553Z
I need to tie into the onLogin event and redirect to a url different from the control panel on login if the user is part of a particular group. Any other ideas how this might be done?
Nicholas Mann
CC BY-SA 3.0
2017-04-27T15:19:34.680Z
Makes sense, I had assumed this was for the front-end. Where is the login form, is it the default CP login page, or your own front-end template. If the latter, then my best guess is `getCsrfInput()` not being set up correctly.
Mike Pierce
CC BY-SA 3.0
2017-04-27T14:59:45.827Z
2017-04-27T15:24:31.363Z
2017-04-27T15:24:31.363Z
Mike Pierce
<p>I have a plugin that checks for the current user on login and should redirect the user if they are part of a particular user group. When I do this I get the following "An unknown error occurred." and the redirect doesn't work. See init function below:</p>

<pre><code>function init()
{
    craft()-&gt;on( 'userSession.onLogin', function( Event $event ){
        $user = craft()-&gt;userSession-&gt;getUser();
        if ($user &amp;&amp; $user-&gt;isInGroup('sales')){
          $url = "new.url";
          craft()-&gt;request-&gt;redirect($url);
        }
    });
}
</code></pre>

<p>Please advise.</p>

CC BY-SA 3.0
2017-04-27T14:55:59.997Z
Have you got dev mode switched on, that might show a bit more information to track down what's going wrong.
Mike Pierce
CC BY-SA 3.0
2017-04-27T15:09:47.193Z
I do, unfortunately it's not giving me any useful info.
Nicholas Mann
CC BY-SA 3.0
2017-04-27T15:17:15.763Z
I'm stumped, the code seems sound. What about the form that you're submitting for the login, do you have `getCsrfInput()` set up correctly?
Mike Pierce
CC BY-SA 3.0
2017-04-27T15:19:07.147Z
I have a custom cpTrigger url, and that's the page I'm using. No alternate login page is being used.
Nicholas Mann
CC BY-SA 3.0
2017-04-27T15:20:39.430Z
Ok, then I'm out of ideas. :(
Mike Pierce
<craft-features>
Redirect user based on group after Craft login
# Redirect user based on group after Craft login

(Asked by: username_0 on 2017-04-27)

I have a plugin that checks for the current user on login and should redirect the user if they are part of a particular user group. When I do this I get the following "An unknown error occurred." and the redirect doesn't work. See init function below:
function init()
{
    craft()->on( 'userSession.onLogin', function( Event $event ){
        $user = craft()->userSession->getUser();
        if ($user && $user->isInGroup('sales')){
          $url = "new.url";
          craft()->request->redirect($url);
        }
    });
}

Please advise.

**Tags:** craft-features
> **Comments:**
> **username_1**: Have you got dev mode switched on, that might show a bit more information to track down what's going wrong.  
> **username_0**: I do, unfortunately it's not giving me any useful info.  
> **username_1**: I'm stumped, the code seems sound. What about the form that you're submitting for the login, do you have `getCsrfInput()` set up correctly?  
> **username_0**: I have a custom cpTrigger url, and that's the page I'm using. No alternate login page is being used.  
> **username_1**: Ok, then I'm out of ideas. :(  

---

## 1 answers

### Answer by username_1 on 2017-04-27. Score: 1

This is an alternate solution to what you're trying to do, and only applies if the login is coming and being directed to front end templates, but you could do something similar directly in your twig template, rather than having to use a plugin.
You could put something like...
{% if currentUser and currentUser.isInGroup('sales') %}
{% redirect "new.url" %}
{% endif %}

...somewhere on the page that logged in users will land on by default.

> **Comments:**
> **username_0**: I need to tie into the onLogin event and redirect to a url different from the control panel on login if the user is part of a particular group. Any other ideas how this might be done?  
> **username_1**: Makes sense, I had assumed this was for the front-end. Where is the login form, is it the default CP login page, or your own front-end template. If the latter, then my best guess is `getCsrfInput()` not being set up correctly.  

---

