Jump to content

Recommended Posts

Posted

Hi all, I would like to redirect the session after successful login for a specific role. I want to keep a specific role in the front-end most of the time and upon successful login, would like to redirect them to the front page instead of the Pages section in the back-end. How would you suggest hooking into the login function and redirecting that role upon successful login? I know I'll need to use $session->redirect(). Thanks!

Posted

Hi Tina,

I've done something similair for a project.

You need an autoload module which executes a code like this:

class YourAutoloadModule extends WireData implements Module {

//...

public function init() {
  
  if ($this->user->hasRole('yourFrontendRole') {
    $this->addHookBefore('ProcessHome::execute', $this, 'redirectToFrontend');
  }
  
}

public function redirectToFrontend(HookEvent $event) {
  $frontend = $this->pages->get('yourPage');
  $this->session->redirect($frontend->url);
}

}
  • Like 1
Posted

Hi Tina,

I've done something similair for a project.

You need an autoload module which executes a code like this:

class YourAutoloadModule extends WireData implements Module {

//...

public function init() {
  
  if ($this->user->hasRole('yourFrontendRole') {
    $this->addHookBefore('ProcessHome::execute', $this, 'redirectToFrontend');
  }
  
}

public function redirectToFrontend(HookEvent $event) {
  $frontend = $pages->get('yourPage');
  $this->session->redirect($frontend->url);
}

}

Hey, I changed "yourFrontendRole" to an actual role but it doesn't work. I get a server error when using that. It's detecting the role because superuser has no problems logging in but upon logging in with my 'standard' role I've created, it causes a 500 error. I think it's having trouble at the $this->session->redirect part.

This is what mine looks like (it's not working)

class LoginRedirect extends WireData implements Module {
	public static function getModuleInfo() {
		return array(
			'title' => 'Login Redirect', 
			'version' => 1, 
			'summary' => '',
			'author' => '',
			'singular' => true, 
			'autoload' => true, 
			);
	}

	public function init() {
		if($this->user->hasRole("standard")) {
			$this->addHookBefore('ProcessHome::execute', $this, 'redirectToFrontend');
		}
	}

	public function redirectToFrontend(HookEvent $event) {
		$this->$session->redirect($pages->get(1)->url);
	}
}
Posted

I had a mistake in my code (corrected). In a module (class), we need to write $this->pages.

So:

$this->session->redirect($this->pages->get(1)->url);

Does it work? :)

Posted

I had a mistake in my code (corrected). In a module (class), we need to write $this->pages.

So:

$this->session->redirect($this->pages->get(1)->url);

Does it work? :)

Thanks but that doesn't work either.

Posted

Tina your example also has an extra '$' before the session variable:

$this->$session->redirect($pages->get(1)->url);

that should be:

$this->session->redirect($this->pages->get(1)->url);

Make sure that you've got $config->debug=true; in your /site/config.php during development so that you get more verbose error messages, which should highlight these things rather than giving you a 500 error. 

I also recommend replacing your "init()" method with "ready()". Technically either should work here, but since $page isn't yet available at init() time, anything that inquires $page would be non-functional. There doesn't appear to be anything here that needs $page, but since you are making a $pages call, I think it's just a little safer to use ready() rather than init(). Note however that you'll want to keep the init() method around, since it is required. But just leave the implementation for it blank, while moving your code to a ready() method. 

Lastly, since this has to do with front-end, you might just want to make your own login form for the front-end. PW's login form is for login to the admin, but it wasn't intended to be used as a front-end login form (even if you can technically do it). You may find it a lot simpler and more flexible just to have a separate form for your front-end login: 

Here's a couple examples:

http://processwire.com/talk/topic/2937-creating-a-front-end-admin/?p=28954

http://processwire.com/talk/topic/1716-integrating-a-member-visitor-login-form/?p=15919

  • Like 2
Posted

You're right, making my own login form got the job done way easier. I should've just done that from the start... The first link helped. Thanks!

  • Like 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...