0

In my application I need do some kind of "auto login" logic at the beginning of app work. In this "auto login" function I do many actions, and one of them - setting cookie, using CookieComponent.

When I use this autoLogin in controller or component - all is fine, BUT cookies are NOT set when I do the same from dispatcher filter.

I dig deep into CakePHP code, and found that when I try set cookie from dispatcher filter, $_cookies property of CakeResponse are empty. So it's looks like dispatcher filter creates own CakeResponse, and it resets later, so cookie are not set.

My filter looks like this:

class UserLoadFilter extends DispatcherFilter 
{
    public $priority = 8;    
    public function beforeDispatch($event) {
        App::uses('AuthComponent', 'Controller/Component');
        if (!AuthComponent::user()) {
            App::uses('CustomAuthComponent', 'Controller/Component');
            //$controller = new AppController($event->data['request'], $event->data['response']);
            $auth = new CustomAuthComponent(new ComponentCollection(), Configure::read('Auth'));
            $auth->autoLogin();
        }
    }
}

I also tried set cookie directly in beforeDispatch method in this way:

App::uses('CookieComponent', 'Controller/Component');
$cookie = new CookieComponent(new ComponentCollection());
$cookie->write('test', 'TEST!', false, "1 day"); 

but this has no sense too.

What do I do wrong? Maybe I just don't see some simple things, but I spent many time and still can't fix this. Is it's possible at all to set cookie from this place?

Sure I can try just use setcookie, or write own cookie wraper, but I want to do it in CakePHP style, using cookie component.

uzyn
  • 6,625
  • 5
  • 22
  • 41
Vovkin
  • 436
  • 1
  • 5
  • 14

1 Answers1

1

This looks just wrong to me. 2.x uses authentication and authorization objects so no CustomAuthComponent - meaning the component part - is needed. Instead you create customized authentication and authorization objects.

I see no reason why this has to be done in beforeDispatcher(). So what exactly is your goal? What kind of auth are you trying to implement?

Edit based on your comment:

Simply redirect after you identified the user as Boris said and your locale gets set (if you did it right). So just read the uuid from the cookie in beforeFilter(), get the user record based on that and use the user record to do a "manual" login and then redirect.

What have you modified in the AuthComponent?

floriank
  • 25,546
  • 9
  • 42
  • 66
  • I try to restore user data and automatically log in him, using value of user UUID from his cookie (but if user has no cookie with UUID value, I create new user entity, and set user UUID to cookie for this user). So, suppose, if user will back to website after monthes, he will be logged in and redirected to his locale. And I want to do it in beforeDispatcher(), because I want to know user locale before routing starts. And about custom authentication object - I use it, but also I need some changes in Auth component too, so I extend it in my CustomAuthComponent. – Vovkin Jul 30 '12 at 07:57
  • 1
    @Vovkin you can redirect to the correct locale after the user is recognized and logged in. This would eliminate the need to do this in `beforeDispatch()`. – Borislav Sabev Jul 30 '12 at 09:18