Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • User authorization in Cakephp

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 122
    Comment on it

    In order to use the auth component. You have to add the component to the AppController. (/app/Controller/AppController.php)

    var $components = array('Auth');
    

    Define $components array in your controller.

    public $components = array(
       'Auth' => array(
         'authenticate' => array(
           'Form' => array (
              'fields' => array('username'=>'email','password'=>'user_password')
            )
         )
       )
    );
    

    You can assign loginredirect and logoutredirect as well as other parameters in beforefilter function in your controller.

    function beforFilter()
    {
      // Display action to be accessed without user login is allowed.
      $this->Auth->allow('display');
    
      //loginRedirect defines the action called after user logged in first-time.
      $this->Auth->loginRedirect = array('controller'=>'users','action'=>'dashboard');
    
      //loginRedirect tell the component where to redirect to user after log-out.
      $this->Auth->logoutRedirect = array('controller'=>'users','action'=>'login');
    }
    

    Now Define login and logout methods in the UsersController file

    function login ()
    {
      //Check user credentials and redirect to user based on that.
      if($this->Auth->login())
      {
        //It automatically redirect to loggedin url which we set earlier above in beforerfilter.
      } 
      else
      {
        //Print a Messege that username or password is wrong if authenticate fails.
        $this->Session->setFlash('Invalid Username or Password,try again');
      }
    }
    
    function logout()
    {
      $this->redirect($this->Auth->logout());
    }
    

    So by using above flow of code you will successfully use auth component in cakephp.

 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: