Jump to content

default Page upon login


evan
 Share

Recommended Posts

Hi,

Is there a way to specify the default Page which appears upon login, either by role or user? I've made a custom admin Page I'd like to appear instead of Pages. Probably am overlooking something simple...

Thanks!

-evan

Link to comment
Share on other sites

This is unknown territory for me...I assume you mean something like this?

public function init() {
$this->addHook('ProcessLogin::afterLoginRedirect', $this, 'helloTest');
}

public function helloTest (HookEvent $event) {
 echo "Hello!";
}

...which didn't work for me.

Link to comment
Share on other sites

I went into the realms of logins and found it isn't possible like this. :)

It's strange even if I change the core ProcessLogin module ___afterLoginRedirect to somthing else, it does nothing. Seems this like it is never used.

Though there a redirect in the module that does the redirect after login and appends an id if one found with get input. So after getting into the logic somehow, I found there's a ProcessHome.module that does only that: redirect to the /page/ when calling /processwire/ login. Though I still don't get it fully, nor do I understand why it is so complicated :) Ryan?

So hooking the ProcessHome.module does work, though if you click the "Admin" breadcrumb it also redirects to the changed page.


class LoginRedirect extends Wire implements Module {

   public static function getModuleInfo() {
       return array(
           'title' => 'After Login Redirect',          
           'summary' => 'Redirect after login', 
           'version' => 100,
           'autoload' => true,
           'permission' => 'page-view'
           );
   }

   public function init() {
       $this->addHookBefore('ProcessHome::execute', $this, 'redirect');
   }

   public function redirect($event){
       $event->replace = true; // replace hooked function completely
       if($this->user->hasRole('editor')){
           $this->session->redirect($this->pages->get(1186)->url);
       } else {
           $this->session->redirect("page/");
       }

   }

}
Link to comment
Share on other sites

You can always create your own login page (and template) and have full control over the flow there. Logging in a user in PW is as simple as calling $session->login('name', 'pass'); If it authenticates, it returns a $user. If it doesn't, it returns null.

The $session->login might also be a good one to hook if you don't want to create your own login form. I think this would be preferable to trying to hook in ProcessLogin or ProcessHome.

Link to comment
Share on other sites

I don't mind the existing login page, I just wanted to change what they land on.

How exactly would I hook into $session->login? The whole hooking concept isn't really clear to me, or at least what is hookable and what is not. Thanks again.

Link to comment
Share on other sites

OK, figured it out:


public function init() {

$this->session->addHook('login', $this, 'on_login');

}

public function on_login (HookEvent $event) {

if ($this->user->hasRole("editor")) { $this->session->redirect($this->pages->get(1065)->url); }

}

Thanks ya'll!

  • Like 3
Link to comment
Share on other sites

  • 2 years later...

Just picking up this old thread. I don't think Evan's solution is applicable anymore but Soma's still seems to work, albeit with the caveats he already spoke about - that every call to admin goes to the new URL.

Just wondering if there is a new hook that will enable a simple module to redirect a user after a login (in my case, back to the front-end).

In the meantime I'll keep trying all the combinations :D

Link to comment
Share on other sites

onjegolders,

This seems a bit weird from a UX perspective — If I follow correctly: 

  1. User authenticates with default PW login page. 
  2. After successful login, user is redirected back to frontend of the site.

Why not just create a simple frontend login script?

If that isn't an option, then part of what I stripped out of the redirects module I posted in the other thread is a different default page for certain roles.

I'll reduce it down to something a bit more generic and post it here in a bit.

Link to comment
Share on other sites

Hi Reno,

The thinking here is that the editor will only really be touching content for the journal. In the past I have done like you said and created front-end forms but in this case, I thought why not take advantage of PW's very robust forms.

I think the workflow is quite nice. They have a login button, they login get redirected back to the journal where they can edit / add posts that also get processed with PW.

It saves me building the forms and validation and it saves the editor having to poke around and choose parents and templates.

(It makes sense in this scenario but I don't think I would use it apart from this "blog" scenario)

Link to comment
Share on other sites

  • 1 year later...

very easy now with newer versions! just put this in your /site/ready.php

$wire->addHookAfter("Session::login", function(HookEvent $event) {
    $user = $event->return;
    // check if login was successful
    if($user) {
        // your condition here
        if(!$user->isSuperuser()) {
            // your destination page here
            wire('session')->redirect('/');
        }
    }
});
  • Like 3
Link to comment
Share on other sites

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
 Share

×
×
  • Create New...