xety/cake3-cookieauth

A simple Cake3 plugin to authenticate users with Cookies.

Installs: 48 316

Dependents: 5

Suggesters: 0

Security: 0

Stars: 18

Watchers: 4

Forks: 13

Open Issues: 6

Type:cakephp-plugin

v1.4 2016-10-25 06:10 UTC

This package is auto-updated.

Last update: 2024-11-05 00:10:44 UTC


README

A simple Cake3 plugin to authenticate users with Cookies. This plugin is based on the awesome plugin FriendsOfCake/Authenticate but with a different setup.

Build Status Coverage Status Scrutinizer Latest Stable Version Total Downloads License

Requirements

  • CakePHP 3.X

Installation

Run : composer require xety/cake3-cookieauth:1.* Or add it in your composer.json:

"require": {
    "xety/cake3-cookieauth": "1.*"
},

Configuration

'Xety/Cake3CookieAuth.Cookie' => [
    'cookie' => [
        'name' => 'CookieAuth'
    ]
]

All others configuration option can be found on the official CakePHP documentation.

Usage

In your config/bootstrap.php add :

Plugin::load('Xety/Cake3CookieAuth');

In your AppController :

public $components = [
    'Cookie',
    'Auth' => [
        'authenticate' => [
            'Form',
            'Xety/Cake3CookieAuth.Cookie'
        ]
    ]

];

In your AppController, in the beforeFilter action :

public function beforeFilter(Event $event) {
    //Automaticaly Login.
    if (!$this->Auth->user() && $this->Cookie->read('CookieAuth')) {

        $user = $this->Auth->identify();
        if ($user) {
            $this->Auth->setUser($user);
        } else {
            $this->Cookie->delete('CookieAuth');
        }
    }
}

//If you want to update some fields, like the last_login_date, or last_login_ip, just do :
public function beforeFilter(Event $event) {
    //Automaticaly Login.
    if (!$this->Auth->user() && $this->Cookie->read('CookieAuth')) {
        $this->loadModel('Users');

        $user = $this->Auth->identify();
        if ($user) {
            $this->Auth->setUser($user);

            $user = $this->Users->newEntity($user);
            $user->isNew(false);

            //Last login date
            $user->last_login = new Time();
            //Last login IP
            $user->last_login_ip = $this->request->clientIp();
            //etc...

            $this->Users->save($user);
        } else {
            $this->Cookie->delete('CookieAuth');
        }
    }
}

In your login action, after $this->Auth->setUser($user); :

//It will write Cookie without RememberMe checkbox
$this->Cookie->configKey('CookieAuth', [
    'expires' => '+1 year',
    'httpOnly' => true
]);
$this->Cookie->write('CookieAuth', [
    'username' => $this->request->data('username'),
    'password' => $this->request->data('password')
]);


//If you want use a RememberMe checkbox in your form :
//In your view
echo $this->Form->checkbox('remember_me');

//In the login action :
if($this->request->data('remember_me')) {
    $this->Cookie->configKey('CookieAuth', [
        'expires' => '+1 year',
        'httpOnly' => true
    ]);
    $this->Cookie->write('CookieAuth', [
        'username' => $this->request->data('username'),
        'password' => $this->request->data('password')
    ]);
}

Contribute

Follow this guide to contribute