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

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 629
    Comment on it

    Hi Reader's,
    Welcome to FindNerd,today we are going to discuss on authenticate application in CakePHP using Auth Component.

    If we are developing a web application in CakePHP then some time we have to use Authenticate user for login functionality, In CakePHP "Auth" component provides these functionality to authenticate a user.

    We can use  authenticate application in our CakePHP application just follow some basic steps which is given below:

    Step-1. In first step we have to create a $components array for using 'Auth' in our AppController.

    <?php
        public $components = array('Session', 'Cookie',
          'Auth' => array('autoRedirect' => false));
        //Here add session and cookies components beacuse when a user is not active from a long time, then CakePHP application will automatically redirect us to UsersController's login action.
        public $components = array( 'Session',
        'Auth' => array(
        'autoRedirect'=>false,
        'loginRedirect' => array(
        'controller'=>'users',
        'action' =>'index'
        ),
        'logoutRedirect' => array(
        'controller'=>'users',
        'action'=>'login' // when the user logs out.
        ),
        )
        ),
        );
    
     ?>

     Step-2: In the second step we have to implement  auth component in our controller (UserControler) for login action and also we have to define those action which we want to user access directly. So for that, we will use beforeFilter() action with adding the allow method.

    Let see how use beforeFilter() ?

    <?php
      public function beforeFilter(){
        parent::beforeFilter();
        $this->Auth->allow('register', 'login','signup');
      }
       
    ?>

    In the above code, there are three actions available 'register', login',' signup' which is accessed by the user directly without any authentication.

    Now we will Create login() action in our UsersController

    <?php
      public function login() {
        if ($this->request->is('post')) {
          if ($this->Auth->login()) {
             return $this->redirect($this->Auth->redirect(array('controller' => 'Posts', 'action' => 'index')));
           }
            //here set the flash massage for showing error massage
            $this->Session->setFlash(('Invalid username or password, please eneter valid username or password'));
        }
      }
      public function logout() {
        $this->Auth->logout();
        $this->redirect(array('controller' => 'users', 'action' => 'login'));//login action
        exit;
      }
    ?>

     

    Step-3 Now in the last step we will create our view page:

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

     I hope this blog will help you to implement authenticate application in you CakePhP application.

 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: