Jump to content


Photo

default Page upon login

solved

  • Please log in to reply
9 replies to this topic

#1 evan

evan

    Jr. Member

  • Members
  • PipPip
  • 42 posts
  • 10

Posted 07 May 2012 - 02:02 AM

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

#2 Soma

Soma

    Hero Member

  • Moderators
  • 3,217 posts
  • 1762

  • LocationSH, Switzerland

Posted 07 May 2012 - 02:20 AM

I think you can hook with a module the "___afterLoginRedirect" in the ProcessLogin.module. Haven't tried but it should be possible.

@somartist | modules created | support me, flattr my work flattr.com


#3 evan

evan

    Jr. Member

  • Members
  • PipPip
  • 42 posts
  • 10

Posted 07 May 2012 - 02:56 AM

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.

#4 Soma

Soma

    Hero Member

  • Moderators
  • 3,217 posts
  • 1762

  • LocationSH, Switzerland

Posted 07 May 2012 - 06:49 AM

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/");
        }
        
    }

}

@somartist | modules created | support me, flattr my work flattr.com


#5 ryan

ryan

    Hero Member

  • Administrators
  • 5,780 posts
  • 3125

  • LocationAtlanta, GA

Posted 07 May 2012 - 10:02 AM

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.

#6 evan

evan

    Jr. Member

  • Members
  • PipPip
  • 42 posts
  • 10

Posted 07 May 2012 - 10:46 AM

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.

#7 evan

evan

    Jr. Member

  • Members
  • PipPip
  • 42 posts
  • 10

Posted 07 May 2012 - 11:13 AM

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!

#8 netcarver

netcarver

    Sr. Member

  • Members
  • PipPipPipPip
  • 428 posts
  • 341

  • LocationUK

Posted 07 May 2012 - 11:48 AM

@evan: Thanks for posting the solution to your problem.
Steve ☧

#9 renobird

renobird

    Sr. Member

  • Members
  • PipPipPipPip
  • 374 posts
  • 237

  • LocationGainesville, Florida

Posted 07 May 2012 - 01:09 PM

Much appreciated here as well @evan. Definitely something I'll make use of.

#10 evan

evan

    Jr. Member

  • Members
  • PipPip
  • 42 posts
  • 10

Posted 07 May 2012 - 01:38 PM

No problem! I'm glad I'm not the only one who's learning...makes me feel less embarrassed about asking questions!





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users