- CakePHP - Discussion
- CakePHP - Useful Resources
- CakePHP - Quick Guide
- CakePHP - File upload
- CakePHP - Date and Time
- CakePHP - Pagination
- CakePHP - Creating Validators
- CakePHP - Validation
- CakePHP - Security
- CakePHP - Cookie Management
- CakePHP - Session Management
- CakePHP - Internationalization
- CakePHP - Form Handling
- CakePHP - Logging
- CakePHP - Errors & Exception Handling
- CakePHP - Services
- CakePHP - Delete a Record
- CakePHP - Update a Record
- CakePHP - View a Record
- CakePHP - Working with Database
- CakePHP - View Events
- CakePHP - View Elements
- CakePHP - Extending Views
- CakePHP - Views
- CakePHP - Controllers
- CakePHP - Routing
- CakePHP - Project Configuration
- CakePHP - Folder Structure
- CakePHP - Installation
- CakePHP - Overview
- CakePHP - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
CakePHP - Services
This 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.
config/routes.php
<?php use CakeCorePlugin; use CakeRoutingRouteBuilder; use CakeRoutingRouter; 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 AppController; use CakeControllerController; use CakeEventEvent; use CakeControllerComponentAuthComponent; class AppController extends Controller { pubpc function initiapze() { parent::initiapze(); $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 ] ]); } pubpc 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 AppController; use AppControllerAppController; use CakeORMTableRegistry; use CakeDatasourceConnectionManager; use CakeEventEvent; use CakeAuthDefaultPasswordHasher; class AuthexsController extends AppController { var $components = array( Auth ); pubpc function index(){ } pubpc 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. ); } } pubpc 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->pnk( 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 cpcking on the logout pnk, you will be redirected to the login screen again.
Advertisements