Authentication in CakePHP

Authentication is the process of identifying the user's identity by providing the necessary details in the login form. The login page of any website usually takes the user's email ID and password to verify the particular person from the number of users' accounts on any web application. If the user has submitted the wrong details of his account, then it will not permit the user to go to the next page of the website. It means the authentication function holds the privacy of the users on the site.

There are three predefined methods for authenticating the user’s data stored in the CakePHP’s application.

FormAuthenticate- This type of authentication is used to authenticate the user data based on the post method of form. It means this function check the authenticity of the user while pass the data through post method of form in login page.

BasicAuthenticate It will allow us to check the authenticity of the user by use of basic HTTP authentication.

DigestAuthenticate It will allow us to check the authenticity of the user by use of digest HTTP authentication.

We have required four files to check the authentication of the user’s data in the web application.

  1. AppController.php file
  2. RegisterController.php file
  3. login.ctp file
  4. Registers.php file

We have to create a database table, which is linked with the model table. In addition, we have to define the number of fields that we want to check for authentication of the user login module.

Here we have created an example for checking the authentication in the login form.

Here we have modify our AppController.php file at src/Controller/AppController.php. In which we have define the fields of the form to authenticate the user identity.

 >?php
 namespaceApp\Controller;
 useCake\Controller\Controller;
 classAppControllerextendsController
 {
 publicfunctioninitialize()
 {
 parent::initialize();
 $this->loadComponent('RequestHandler',[
 'enableBeforeRedirect'=>false,
 ]);
 $this->loadComponent('Flash'); // load the Flash component to show the information
 $this->loadComponent('Auth',[ // declare the auth function globally for //verifying the authentication in login page
 'authenticate'=> [
 'Form'=> [
 'fields'=>[ 
 'uname'=>'email', // describe the fields for performing authentication 
 'password'=>'password',
 ]
 ]
 ],
 'loginAction'=>[
 'controller'=>'Registers',
 'action'=>'login', // You can redirect the login action by pass //the controller and login action
 ]
 ]);
 }
 }
 ?> 

Now we will create RegistersController.php file in our controller folder at src/Controller/RegistersController.php file.

 >?php
 namespaceApp\Controller;
 classRegistersControllerextendsAppController
 {public$row;
 publicfunctioninitialize()
 {
 parent::initialize();
 $this->loadComponent('Flash');
 }
 publicfunctionlogin()
 {
 if($this->request->is('post'))
 {
 $row=$this->Auth->identify();
 if($row){
 $this->Auth->setUser($row);
 $this->Flash->success ('YouhaveSuccessfullylogin',['key'=>'message']);
 return$this->redirect($this->Auth->redirectUrl());
 }
 $this->Flash->error ('Yourusernameorpasswordisincorrect');
 }
 }
 }
 ?> 

Now we will create a Registers Folder in src/Template/ and that folder we have to create an action file as login.ctp file.

Here is our login.ctp file and write the given below code in the login.ctp file

 <br><br>
 <div class="row">
 <?php 
 echo $this->Form->create();
 echo $this->Form->controls(
  [ 
  'email'=> ['required'=>true, 'placeholder'=>'Enter your Email','type'=>'email'],
  'password'=>['required'=>true, 'placeholder'=>'Enter your password'],
  ],
  ['legend'=>'Login Here']
  );
  echo $this->Form->button('Submit');
 echo $this->Form->end(); 
  ?>
  </div>
 <?php 

Now we will create a RegistersTable.php file in src/Model/Table/RegistersTable.php for creating the database table that links with the RegistersTable.php file.

 >?php
 namespace App\Model\Table;
 use Cake\Validation\Validator;
 use Cake\ORM\Table;
 class RegistersTable extends Table
 {
  public function initialize(array $config)
  { //$this->addBehavior('Timestamp'); // You can pass here addition things like as date
  }
 }
 ?> 

Now when we run our code in localhost by writing this localhost/CakePHP3.8/Registers/login

Then it will show the given below output.

Authentication in CakePHP



ADVERTISEMENT
ADVERTISEMENT