benedmunds / CodeIgniter-Ion-Auth

Simple and Lightweight Auth System for CodeIgniter
http://benedmunds.com/ion_auth/
MIT License
2.34k stars 1.14k forks source link

Can't seem to be able to use the setHook function #1515

Closed gabe152006 closed 3 years ago

gabe152006 commented 3 years ago

This Github Issue section is meant to track bugs with the library itself Please post generic support issues to the CodeIgniter forums or StackOverflow.

Using branch 4 CI 4.1

Hello!

I'm trying to use inject the following function using the "post_login_successful" hook after login and it doesn't seem to work.

....
if ($this->ionAuth->login($this->request->getVar('identity'), $this->request->getVar('password'), $remember))
{
    //if the login is successful
    //redirect them back to the home page
    $this->ionAuth->setHook("post_login_successful", "check_cookie", get_class($this), "handleCookies", array());
....

The function I'm trying to call

public function handleCookies() {
     setcookie("test123","tester",3600);
}

Seems like it the setHook gets ignored and continues through the normal login process. I'm wondering if I should insert the hook in a different part of the login process?

benedmunds commented 3 years ago

You'll need to set the hook before calling the login() method.

gabe152006 commented 3 years ago

You'll need to set the hook before calling the login() method.

Sorry, I'm a bit confused, call the hook in a different method in the Auth controller file? or in the construct?

benedmunds commented 3 years ago

You can keep it here but you'll need to move the setHook() call up a few lines. Since the hook is called in the login() method it has to be set before the login() method is called.

Something like this:

$this->ionAuth->setHook("post_login_successful", "check_cookie", get_class($this), "handleCookies", array());
if ($this->ionAuth->login($this->request->getVar('identity'), $this->request->getVar('password'), $remember))
{
    //if the login is successful
    //redirect them back to the home page
gabe152006 commented 3 years ago

You can keep it here but you'll need to move the setHook() call up a few lines. Since the hook is called in the login() method it has to be set before the login() method is called.

Something like this:

$this->ionAuth->setHook("post_login_successful", "check_cookie", get_class($this), "handleCookies", array());
if ($this->ionAuth->login($this->request->getVar('identity'), $this->request->getVar('password'), $remember))
{
    //if the login is successful
    //redirect them back to the home page

I see! Looks like now it did execute the code after I moved it but it threw this error:

call_user_func_array(): Argument #1 ($callback) must be a valid callback, non-static method App\Controllers\Auth::handleCookies() cannot be called statically

Any insight?

Trace:

VENDORPATH/benedmunds/codeigniter-ion-auth/Models/IonAuthModel.php at line 2317

2310      */
2311     protected function callHook(string $event, string $name)
2312     {
2313         if (isset($this->ionHooks->{$event}[$name]) && method_exists($this->ionHooks->{$event}[$name]->class, $this->ionHooks->{$event}[$name]->method))
2314         {
2315             $hook = $this->ionHooks->{$event}[$name];
2316 
2317             return call_user_func_array([$hook->class, $hook->method], $hook->arguments);
2318         }
2319 
2320         return false;
2321     }
2322 
2323     /**
2324      * Call Additional functions to run that were registered with setHook().
benedmunds commented 3 years ago

I think I see the problem. I just pushed a commit to the 4 branch, update to use that and then update this code to:

$this->ionAuth->setHook("post_login_successful", "check_cookie", $this, "handleCookies", array());

We need to pass the instantiated object instead of just the name, but the previous typing wouldn't allow that.

gabe152006 commented 3 years ago

I think I see the problem. I just pushed a commit to the 4 branch, update to use that and then update this code to:

$this->ionAuth->setHook("post_login_successful", "check_cookie", $this, "handleCookies", array());

We need to pass the instantiated object instead of just the name, but the previous typing wouldn't allow that.

Ahh thank you so much! The problem is now fixed

benedmunds commented 3 years ago

Awesome, take care!