Skip to content

Instantly share code, notes, and snippets.

@archanavhire
Last active August 29, 2015 14:02
Show Gist options
  • Save archanavhire/842266d5953c0df413f2 to your computer and use it in GitHub Desktop.
Save archanavhire/842266d5953c0df413f2 to your computer and use it in GitHub Desktop.
CakePHP-3 Authentication
<!-- app/View/Users/add.ctp -->
<div class="users form">
<?= $this->Form->create($user) ?>
<fieldset>
<legend><?= __('Add User') ?></legend>
<?= $this->Form->input('email') ?>
<?= $this->Form->input('password') ?>
</fieldset>
<?= $this->Form->button(__('Submit')); ?>
<?= $this->Form->end() ?>
</div>
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since 0.2.9
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace App\Controller;
use Cake\Controller\Controller;
/**
* Application Controller
*
* Add your application-wide methods in the class below, your controllers
* will inherit them.
*
* @link http://book.cakephp.org/3.0/en/controllers.html#the-app-controller
*/
class AppController extends Controller {
public $components = [
'Session',
'Auth' => [
'loginRedirect' => [
'controller' => 'Articles',
'action' => 'index'
],
'logoutRedirect' => [
'controller' => 'Pages',
'action' => 'display',
'home'
],
'authenticate' => [
'Form' => [
'fields' => ['username' => 'email'],
'passwordHasher' => [
'className' => 'Simple',
]
]
]
]
];
public function beforeFilter() {
$this->Auth->allow(['index', 'view']);
}
}
<div class="users form">
<?= $this->Session->flash('auth') ?>
<?= $this->Form->create() ?>
<fieldset>
<legend><?= __('Please enter your username and password') ?></legend>
<?= $this->Form->input('email') ?>
<?= $this->Form->input('password') ?>
</fieldset>
<?= $this->Form->button(__('Login')); ?>
<?= $this->Form->end() ?>
</div>
<?php
namespace App\Model\Entity;
use Cake\ORM\Entity;
use Cake\Auth\SimplePasswordHasher;
class User extends Entity {
protected function _setPassword($password) {
return (new SimplePasswordHasher)->hash($password);
}
}
?>
<?php
// App/Controller/UsersController.php
namespace App\Controller;
use App\Controller\AppController;
use Cake\Error\NotFoundException;
class UsersController extends AppController {
public function beforeFilter() {
parent::beforeFilter();
// Allow users to register and logout.
$this->Auth->allow(['add', 'logout']);
}
public function login() {
if ($this->request->is('post')) {
if ($this->Auth->login()) {
return $this->redirect($this->Auth->redirect());
}
$this->Session->setFlash(__('Invalid username or password, try again'));
}
}
public function logout() {
return $this->redirect($this->Auth->logout());
}
public function index() {
$this->set('users', $this->Users->find('all'));
}
public function view($id) {
if (!$id) {
throw new NotFoundException(__('Invalid user'));
}
$user = $this->Users->get($id);
$this->set(compact('user'));
}
public function add() {
$user = $this->Users->newEntity($this->request->data);
if ($this->request->is('post')) {
if ($this->Users->save($user)) {
$this->Session->setFlash(__('The user has been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Session->setFlash(__('Unable to add the user.'));
}
$this->set('user', $user);
}
}
?>
<?php
// App/Model/Table/UsersTable.php
namespace App\Model\Table;
use Cake\ORM\Table;
class UsersTable extends Table {
public function validationDefault(Validator $validator) {
return $validator
->notEmpty('email', 'A email is required')
->notEmpty('password', 'A password is required');
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment