How to use Auth login in cakephp

Cakephp

Posted on 25 Feb 16 by Vickey
  728



Cakephp make our project development very fast. There are many inbuilt functionalities or components. By using these functionalities or functions we can do more in less code if we know about those functionalities very well. Some times we know about the functions or components  but don't know how to use it. For example 'Auth' component. Many new comers in Cakephp don't know how to use it. They make a little mistake and auth login not work properly. Here I will explain and create a simple Auth login in Cakephp.

First of all we need add 'Auth' component in component list. Here I am using it in 'AppController' so that it will work in whole application.

class AppController extends Controller {

	public $components = array('Session','Auth');

}

By default it will use 'User' Model and login fields 'username' and 'password'. But if we are doing some different we can use

public $components = array('Session','Auth' => array(

	'authenticate' => array(

		'Form' => array( 

			'userModel' => 'User',

        	'fields' => array(

                'username' => 'email',
                'password' => 'password'
                
            ),
			#'scope' => array('is_active' => '1')
        )
	),
),
);

By using 'Auth' component we can not access any method with out login. But we need to access login and register actions without logged in. So we can access these by using below code in our controller

public function beforeFilter() {

	parent::beforeFilter();
	$this->Auth->allow('login','register');

} 

Second step Encrypt your password by using AuthComponent. We will save encrypted password in our database table. So we will encrypt it before save in database by using the below code in our Model.

public function beforeSave($options = array()) {
    
    if (isset($this->data[$this->alias]['password'])) {
        $this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']);
    }
    return true;
}

Now when any user will be register, his password will be save in encrypted form. After register a user we will try to login by using 'Auth' component. For login add the below code in your controller

public function login() {
	
	if($this->request->is('post')){
		if($this->Auth->login()){
			$this->redirect(array('controller'=>'pages','action' => 'home'));
		}
		else
		{
			$this->Session->setFlash(__('Invalid username or password, Please try again.', null),  'default', 
			array('class' => 'errorDisplay'),'login');
		}
	}
}

For logout

public function logout() {
	$this->Auth->logout();
	$this->redirect(array('controller'=>'users', 'action'=>'login'));
}

Now create a login form for login action in 'app/users/login.ctp


 
Form->create('User'); ?>

Session->flash('login'); ?> Form->input('email'); echo $this->Form->input('password'); ?>

Form->end(__('Submit')); ?>

Now you can access login page and login by using URL "your base url"/users/login'

You can download source code here


If this post help you reduce time to develop, you can help me to maintain this website :)
www.000webhost.com