cakephp authentication


cakephp authentication

appconrtoller:

class AppController extends Controller {
/**
* Helpers will be used
*
* @var array
*/
public $helpers = array(‘Html’, ‘Form’, ‘Session’, ‘Text’, ‘Js’); //, ‘GeneralFunctions’
/**
* Components will be used
*
* @var array
*/
public $components = array(‘Session’, ‘RequestHandler’, ‘Paginator’, ‘Auth’, ‘Cookie’, ‘Email’);

function beforeFilter() {
parent::beforeFilter();

$this->Auth->authenticate = array(‘all’ => array(‘scope’ => array(‘User.user_type’ => array(‘user’), ‘User.is_blocked’ => 0,’User.status’ => 1)), ‘Form’ => array(‘fields’ => array(‘username’ => ’email’, ‘password’ => ‘password’)));
$this->Auth->loginAction =  array(‘plugin’ => false, ‘controller’ => ‘users’, ‘action’ => ‘login’);
$this->sessiondata = ‘Auth.User’;
$loginRedirect =  ‘dashboard’;
$this->Auth->loginRedirect = array(‘plugin’ => false, ‘controller’ => ‘users’, ‘action’ => ‘dashboard’);

}

 

}

user conroller

class UsersController extends UsersAppController {

public $uses = array(‘Users.User’);
public $helpers = array(‘Html’, ‘Form’);
public $components = array(‘Auth’);

function beforeFilter() {
parent::beforeFilter();
$this->set(‘model’, $this->modelClass);

$this->Auth->allow(‘register’, ‘login’,’admin_login’);

}

function login() {

if($this->Auth->loggedIn()){

$this->redirect(array(‘plugin’=>’users’,’controller’=>’users’,’action’=>’dashboard’));

}

if (!empty($this->data)) {

$this->{$this->modelClass}->set($this->data);
if ($this->{$this->modelClass}->loginvalidation()) {

if ($this->Auth->login()) {

return $this->redirect($this->Auth->redirect());
}
} else {

}
} else {

}
}

function dashboard(){

}

function logout(){
return $this->redirect($this->Auth->logout());

}

}

 

Usser model

 

<?php

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

/**
* CakePHP User
* @author vijayraj
*/
App::uses(‘AppModel’, ‘Model’);

class User extends AppModel {

public $uses = ‘User’;

//validation for login form

function loginvalidation() {
$validation1 = array(
’email’ => array(
’empty’ => array(
‘rule’ => ‘notEmpty’,
‘message’ => ‘error_name’
),
‘valid’=>array(
‘rule’=>’email’,
‘message’=>’please enter valid email’

),

),

‘password’ => array(
’empty’ => array(
‘rule’ => ‘notEmpty’,
‘message’ => ‘error_name’
)
),

);
$this->validate = $validation1;
return $this->validates();
}

}

 

login.ctp

 
<div class=”row”>

<?php echo $this->form->create($model,array(‘class’=>’form-horizontal’)); ?>
<div class=”form-group”>

<?php echo $this->Form->label($model.’.email’,__d(‘default’,’email’),array(‘class’=>’col-sm-2 control-label’));  ?>
<div class=”col-sm-10″>

<?php echo  $this->Form->text($model.’.email’,array(‘class’=>’form-control’,’label’=>false,’required’=>false,’error’=>false)); ?>
<?php  echo $this->form->error($model.’.email’); ?>
</div>
</div>
<div class=”form-group”>
<?php echo $this->Form->label($model.’.password’,__d(‘default’,’password’),array(‘class’=>’col-sm-2 control-label’));  ?>
<div class=”col-sm-10″>

<?php echo  $this->Form->password($model.’.password’,array(‘class’=>’form-control’,’label’=>false,’required’=>false,’error’=>false)); ?>
<?php echo $this->form->error($model.’.password’); ?>
</div>
</div>

<div class=”form-group”>
<div class=”col-sm-offset-2 col-sm-10″>

<?php echo  $this->Form->submit(‘Sign in’,array(‘class’=>’btn btn-default’)); ?>
</div>
</div>
<?php echo  $this->Form->end(); ?>

</div>

 

 

Leave a comment