RememberMe component, the final word

Posted in CakePHP on 01.07.2008.

This is really starting to be annoying, but it is also showing how much one can evolve by learning CakePHP in more detail.

I promise, this is the last version of this component. When I first started messing with the "remember me" on a user login, I never thought I'd write this much text on the topic.

Anyway, the previous version was/is working, but thanks to Baz L, I've decided to revisit this component again. And there was much rejoicing.

First of all, this new version is so simple, your head might explode.

Here it is, in all it's simplicity:

class RememberMeComponent extends Object
{
    var $components = array('Auth', 'Cookie');
    var $controller = null;

    /**
     * Cookie retention period.
     *
     * @var string
     */
    var $period = '+2 weeks';
    var $cookieName = 'User';

    function startup(&$controller)
    {
        $this->controller =& $controller;
    }

    function remember($username, $password)
    {
        $cookie = array();
        $cookie[$this->Auth->fields['username']] = $username;
        $cookie[$this->Auth->fields['password']] = $password;

        $this->Cookie->write(
            $this->cookieName,
            $cookie,
            true,
            $this->period
        );
    }

    function check()
    {
        $cookie = $this->Cookie->read($this->cookieName);

        if (!is_array($cookie) || $this->Auth->user())
            return;

        if ($this->Auth->login($cookie))
        {
            $this->Cookie->write(
                $this->cookieName,
                $cookie,
                true,
                $this->period
            );
        }
        else
        {
            $this->delete();
        }
    }

    function delete()
    {
        $this->Cookie->del($this->cookieName);
    }
}

So there. I'm letting Auth worry about the model and the username & password fields. The usage of this component remains the same:

class UsersController extends AppController
{
    var $name = 'Users';
    var $uses = array('User');

    function login()
    {
        if (!$this->Auth->user())
        {
            return;
        }

        if (empty($this->data))
        {
            $this->redirect($this->Auth->redirect());
        }

        if (empty($this->data['User']['remember_me']))
        {
            $this->RememberMe->delete();
        }
        else
        {
            $this->RememberMe->remember
                (
                    $this->data['User']['username'],
                    $this->data['User']['password']
                );
        }

        unset($this->data['User']['remember_me']);
        $this->redirect($this->Auth->redirect());
    }

    function logout()
    {
        $this->RememberMe->delete();
        $this->redirect($this->Auth->logout());
    }
}

And the app controller:

class AppController extends Controller
{
    var $components = array('RememberMe');

    function beforeFilter()
    {
        // again, you can customize the component settings here,
        // like this:
        // $this->RememberMe->period = '+2 months';
        // $this->RememberMe->cookieName = 'Gobshite';

        $this->RememberMe->check();
    }
}   

That's pretty much it. I hope I won't discover a new mega-bull in this code, I don't want to go around the Europe again, hitting myself with a plank and saying things like Pie Jesu Domine, Donna eis requiem...

The component is available for download here:

Happy baking!

Article comments — View · Add


No comments!