Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Cakephp - Using Auth

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 129
    Comment on it

    We can use Auth component in cakephp for authentication and authorization objects. Since it easy and secure way to do this.

    Write following in your App controller

    public $components = array(
                'Auth' => array('loginAction' => array('controller' => 'users', 'action' => 'login'),
                    'logoutRedirect' => array('controller' => 'pages', 'action' => 'home'),
                    'authError' => 'You don\'t have access here.',
                )
            );
    

    Following in your User controller

    //LOGIN

    public function login() {
         if ($this->request->is('post')) {
            $this->Auth->authenticate = array(
                'Form' => array(
                    'userModel' => 'TableName',//Employer,Athlete or etc 
                )
            );
            if ($this->Auth->login()) {
                $this->redirect(array('controller' => 'users', 'action' => 'profile'));
            } else {
                $this->Session->setFlash(__('Invalid username or password, try again'));
                $this->redirect(array('controller' => 'Pages', 'action' => 'home#toLogin'));
            }
        }
    }
    

    //LOGOUT

    public function logout() {
        $this->Auth->autoRedirect = false;
        $this->Auth->logout();
        $this->redirect(array('controller' => 'pages', 'action' => 'home'));
    }
    

 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: