Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Authenticate Application In CakePHP Using Auth Component

    • 0
    • 3
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 4.12k
    Comment on it

    In CakePHP,"Auth" component provides Login functionality to authenticate a user.

    You just Follow the below Steps to develop authenticate application in CakePHP.

    Step-1. To Use 'Auth' in AppController, you have to create $components array

    public $components = array('Session', 'Cookie', 
       'Auth' => array(
       'autoRedirect' => false
    ));
    
    //add Session and cookies components as well beacuse When a user is inactive from a long time, then CakePHP application will automatically redirect you to UsersController's login action or method.
    
    
    public $components = array( 'Session',
        'Auth' => array(  
                'autoRedirect'=>false,
                'loginRedirect' => array(
                    'controller'=>'users',
                    'action' =>'index'
                ),
              'logoutRedirect' => array(
                'controller'=>'users',
                'action'=>'login'     //logoutRedirect tells the system where to redirect when the user logs out.
            ),
        )
      ),
    );
    

    Step-2: Implement the Auth component in UsersController login action.

    First we have to define those action that we want the user to access directly. we can do this by adding the allow method in beforeFilter() action

    public function beforeFilter()
    {
      parent::beforeFilter(); 
      $this->Auth->allow('register', 'login','contact');   
    }
    //there are three actions  'register',login','contact' which can access by user directly without any authentication.
    

    Now we have to Create login() action in our UsersController controller class

     public function login() {
            if ($this->request->is('post')) {
                if ($this->Auth->login()) {
                    return $this->redirect($this->Auth->redirect(array('controller' => 'Posts', 'action' => 'index')));
                }
                $this->Session->setFlash(('Invalid username or password, try again'));
            }
        }
    
    public function logout() {
      $this->Auth->logout();
      $this->redirect(array('controller' => 'users', 'action' => 'login'));
      exit;
    }
    

    Step-3: Now you have to create the presentation file for the login (i.e login.ctp).

    <?php
    echo $this->Form->create('User', array('id'=>'loginform'));
    echo $this->Form->input('User.username', array('type'=>'text','id'=>'username', 
    'label'=>'Username<sup>*</sup>'));
    echo $this->Form->input('User.password', array('type'=>'password', 'id'=>'password', 
    'label'=>'Password<sup>*</sup>'));
    echo $this->Form->submit('Login', array('class'=>'submit', 'div'=>false));
    echo $this->Form->end(); ?>
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: