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

Please advise.

  • Have you got dev mode switched on, that might show a bit more information to track down what's going wrong. Apr 27 '17 at 14:55
  • I do, unfortunately it's not giving me any useful info. Apr 27 '17 at 15:09
  • 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? Apr 27 '17 at 15:17
  • I have a custom cpTrigger url, and that's the page I'm using. No alternate login page is being used. Apr 27 '17 at 15:19
  • Ok, then I'm out of ideas. :( Apr 27 '17 at 15:20

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.

  • 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? Apr 27 '17 at 15:16
  • 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. Apr 27 '17 at 15:19

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.