Authenticate plugin
, (*1)
NOTE: This project is no longer maintained actively.
The Authenticate classes have become redundant or better alternatives have surfaced:
* MultiColumnAuthenticate, see Tools - or use custom finders in CakePHP 3
* CookieAuthenticate, see Xety/Cake3-CookieAuth
* TokenAuthenticate, see JwtAuth, (*2)
Plugin containing some authenticate classes for AuthComponent., (*3)
Current classes:
* MultiColumnAuthenticate, allow login with multiple db columns in single username field
For example username or email
* CookieAuthenticate, login with a cookie
* TokenAuthenticate, login with a token as url parameter or header, (*4)
Requirements
Installation
[Composer], (*5)
run: composer require friendsofcake/authenticate:dev-cake3
or
add "friendsofcake/authenticate":"dev-cake3"
to require
section in your
application's composer.json
., (*6)
Usage
In your app's config/bootstrap.php
add: Plugin::load('FOC/Authenticate');
, (*7)
Configuration:
Setup the authentication class settings, (*8)
MultiColumnAuthenticate:
//in $components
public $components = [
'Auth' => [
'authenticate' => [
'FOC/Authenticate.MultiColumn' => [
'fields' => [
'username' => 'login',
'password' => 'password'
],
'columns' => ['username', 'email'],
'userModel' => 'Users',
'scope' => ['Users.active' => 1]
]
]
]
];
// Or in beforeFilter()
$this->Auth->config('authenticate', [
'FOC/Authenticate.MultiColumn' => [
'fields' => [
'username' => 'login',
'password' => 'password'
],
'columns' => ['username', 'email'],
'userModel' => 'Users',
'scope' => ['Users.active' => 1]
]
]);
CookieAuthenticate:
//in $components
public $components = [
'Auth' => [
'authenticate' => [
'FOC/Authenticate.Cookie' => [
'fields' => [
'username' => 'login',
'password' => 'password'
],
'userModel' => 'SomePlugin.Users',
'scope' => ['User.active' => 1]
]
]
]
];
//Or in beforeFilter()
$this->Auth->authenticate = [
'FOC/Authenticate.Cookie' => [
'fields' => [
'username' => 'login',
'password' => 'password'
],
'userModel' => 'SomePlugin.Users',
'scope' => ['Users.active' => 1]
]
];
Setup both:
It will first try to read the cookie, if that fails will try with form data:, (*9)
//in $components
public $components = [
'Auth' => [
'authenticate' => [
'FOC/Authenticate.Cookie' => [
'fields' => [
'username' => 'login',
'password' => 'password'
],
'userModel' => 'SomePlugin.Users',
'scope' => ['User.active' => 1]
],
'FOC/Authenticate.MultiColumn' => [
'fields' => [
'username' => 'login',
'password' => 'password'
],
'columns' => ['username', 'email'],
'userModel' => 'Users',
'scope' => ['Users.active' => 1]
]
]
]
];
Setting the cookie
Example for setting the cookie:, (*10)
<?php
App::uses('AppController', 'Controller');
/**
* Users Controller
*
* @property User $User
*/
class UsersController extends AppController {
public $components = ['Cookie'];
public function login() {
if ($this->request->is('post')) {
$user = $this->Auth->identify();
if ($user) {
$this->Auth->setUser($user);
$this->_setCookie();
return $this->redirect($this->Auth->redirectUrl());
}
$this->Flash->error(__('Invalid username or password, try again'));
}
}
protected function _setCookie() {
if (!$this->request->data('remember_me')) {
return false;
}
$data = [
'username' => $this->request->data('username'),
'password' => $this->request->data('password')
];
$this->Cookie->write('RememberMe', $data, true, '+1 week');
return true;
}
}
TokenAuthenticate
//in $components
public $components = [
'Auth' => [
'authenticate' => [
'FOC/Authenticate.Token' => [
'parameter' => '_token',
'header' => 'X-MyApiTokenHeader',
'userModel' => 'Users',
'scope' => ['Users.active' => 1],
'fields' => [
'username' => 'username',
'password' => 'password',
'token' => 'public_key',
],
'continue' => true
]
]
]
];
//Or in beforeFilter()
$this->Auth->config('authenticate', [
'FOC/Authenticate.Token' => [
'parameter' => '_token',
'header' => 'X-MyApiTokenHeader',
'userModel' => 'Users',
'scope' => ['Users.active' => 1],
'fields' => [
'username' => 'username',
'password' => 'password',
'token' => 'public_key',
],
'continue' => true
]
]);