In this CakePHP Services chapter deals with the information about the authentication process available in CakePHP.
Authentication
Authentication is the process of identifying the correct user. CakePHP supports three types of authentication.
- FormAuthenticate − It allows you to authenticate users based on form POST data. Usually, this is a login form that users enter information into. This is default authentication method.
- BasicAuthenticate − It allows you to authenticate users using Basic HTTP authentication
- DigestAuthenticate − It allows you to authenticate users using Digest HTTP authentication.
Example for FormAuthentication
Make changes in the config/routes.php file as shown in the following code of CakePHP Services.
config/routes.php
<?php use Cake\Core\Plugin; use Cake\Routing\RouteBuilder; use Cake\Routing\Router; Router::defaultRouteClass('DashedRoute'); Router::scope('/', function (RouteBuilder $routes) { $routes->connect('/auth',['controller'=>'Authexs','action'=>'index']); $routes->connect('/login',['controller'=>'Authexs','action'=>'login']); $routes->connect('/logout',['controller'=>'Authexs','action'=>'logout']); $routes->fallbacks('DashedRoute'); }); Plugin::routes();
Change the code of AppController.php file as shown in the following program.
src/Controller/AppController.php
<?php namespace App\Controller; use Cake\Controller\Controller; use Cake\Event\Event; use Cake\Controller\Component\AuthComponent; class AppController extends Controller { public function initialize() { parent::initialize(); $this->loadComponent('RequestHandler'); $this->loadComponent('Flash'); $this->loadComponent('Auth', [ 'authenticate' => [ 'Form' => [ 'fields' => [ 'username' => 'username', 'password' => 'password' ] ] ], 'loginAction' => [ 'controller' => 'Authexs', 'action' => 'login' ], 'loginRedirect' => [ 'controller' => 'Authexs', 'action' => 'index' ], 'logoutRedirect' => [ 'controller' => 'Authexs', 'action' => 'login' ] ]); } public function beforeFilter(Event $event) { $this->Auth->allow(['index','view']); $this->set('loggedIn', $this->Auth->user()); } }
Create AuthexsController.php file at src/Controller/AuthexsController.php. Copy the following code in the controller file.
src/Controller/AuthexsController.php
<?php namespace App\Controller; use App\Controller\AppController; use Cake\ORM\TableRegistry; use Cake\Datasource\ConnectionManager; use Cake\Event\Event; use Cake\Auth\DefaultPasswordHasher; class AuthexsController extends AppController { var $components = array('Auth'); public function index(){ } public function login(){ if($this->request->is('post')) { $user = $this->Auth->identify(); if($user){ $this->Auth->setUser($user); return $this->redirect($this->Auth->redirectUrl()); } else $this->Flash->error('Your username or password is incorrect.'); } } public function logout(){ return $this->redirect($this->Auth->logout()); } } ?>
Create a directory Authexs at src/Template and under that directory create a View file called login.php. Copy the following code in that file.
src/Template/Authexs/login.php
<?php echo $this->Form->create(); echo $this->Form->control('username'); echo $this->Form->control('password'); echo $this->Form->button('Submit'); echo $this->Form->end(); ?>
Create another View file called logout.php. Copy the following code in that file.
src/Template/Authexs/logout.php
You are successfully logged out.
Create another View file called index.php. Copy the following code in that file.
src/Template/Authexs/index.php
You are successfully logged in. <?php echo $this->Html->link('logout',[ "controller" => "Authexs","action" => "logout" ]); ?>
Execute the above example by visiting the following URL.
http://localhost/cakephp4/auth
Output
As the authentication has been implemented, and once you try to visit the above URL, you will be redirected to the login page as shown below.
After providing the correct credentials, you will be logged in and redirected to the screen as shown below
After clicking on the logout link, you will be redirected to the login screen again.
Next Topic : Click Here