3

Is there a way to send the user to a different page than the control panel when logging in from a custom login form?

I've tried..

<form method="post" action="" accept-charset="UTF-8">
    <input type="hidden" name="action" value="users/login">
    <input type="hidden" name="redirect" value="apply">

I've also tried setting the returnUrl session in a login event within a plugin. There's a bit left to do in the init function but you get the idea. Currently the code should redirect all logins.

public function init()
{
    craft()->on('userSession.onLogin', function(Event $event) {
        // load the user info and determine user group
        // if part of applicant user group
        // load any existing unfinished applications
        // set a session for the unfinished application
        // redirect to /apply which should load the unfinished application
        craft()->session['returnUrl'] = "/apply";
    });
}
  • In the end this ended up being a small error with the path in the returnUrl. Simply changing it to craft()->session['returnUrl'] = "apply"; appears to have solved the problem. I'm still curious if there's a way to set this in the custom login template or if it has to be done in a plugin. – Anson LeClair Nov 4 '14 at 19:27
4

You can set a variable in your craft/config/general.php. Here is the option taken from the default config file which is located here craft/app/etc/config/defaults/general.php:

Updated:

/**
 * The path that users should be redirected to after logging in from the front-end site.
 *
 * This setting will also come into effect if the user visits the Login page (as specified by the loginPath config
 * setting) when they are already logged in.
 */
'postLoginRedirect' => '',

From that page you can also easily route them to another page based on conditionals in twig using {% if something %}{% redirect url %}{% endif %}.

And although I haven't tried this personally, it looks like (based on this answer) you can redirect from within your plugin using the following (perhaps only from a controller however):

$this->redirect('/apply');

I also see in UserSessionService.php line 371 the following which should also work:

$url = UrlHelper::getUrl(craft()->config->getLoginPath());
craft()->request->redirect($url);
  • Isn't account activation when an account is initially activated? Or does it refer to when the user logs in? Is there a way to set different redirect paths for different forms with the config file method? – Anson LeClair Nov 4 '14 at 18:54
  • Unfortunately $this->redirect('/apply'); did not work in my plugin. Perhaps $this->redirect() can only be used in a controller. It throws errors when used in an init() function within the main plugin file. – Anson LeClair Nov 4 '14 at 19:29
  • You're right on both counts. Sorry. I grabbed the wrong variable. Use postLoginRedirect instead. I have updated the answer. – Douglas McDonald Nov 4 '14 at 21:59

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.