4

I am working on cakephp 2.x . my problem is i dont want to use username for logging .. i am taking the email and password from the user and verify this email and password from the database i have a table in my database name user and it has 3 fields id, email and password

here is my code

Model

 <?php
class User extends AppModel {
public $useTable = 'user';
}
?>

AppController

 class AppController extends Controller {
   public $components = array(
   'Session',
'Auth'=>array(
    'loginRedirect'=>array('controller'=>'users', 'action'=>'admin'),
    'logoutRedirect'=>array('controller'=>'users', 'action'=>'admin'),
    'authError'=>"You can't access that page",
    'authorize'=>array('Controller') 
   )
);

public function isAuthorized($user) {
}

  public function beforeFilter() {
  $this->Auth->allow('index');

UserController

public function login()
   {
     if ($this->request->is('post')) {
       if ($this->Auth->login()) {
        $this->redirect($this->Auth->redirect());
    } else {
        $this->Session->setFlash('Your email/password combination was incorrect');
    }
}
}

login.ctp

 <?php


   echo $this->form->create();

  echo $this->form->input('email');
  echo $this->form->input('password');


    echo $this->form->end('Authenticate');
   ?>
Android Developer
  • 987
  • 1
  • 8
  • 22
hellojohn
  • 109
  • 1
  • 3
  • 12

1 Answers1

9

You can configure it with:

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

See also Configuring Authentication handlers in the cookbook.

dhofstet
  • 9,934
  • 1
  • 36
  • 37
  • 1
    sorry 2 ask this silly question..where should i add your code ? or please if you can adjust your code within my code so i can instantly run this and inform you about the output – hellojohn Jun 15 '13 at 05:22
  • It's just a new entry with the key `authenticate` in the `Auth` array you already have in your `AppController`. – dhofstet Jun 15 '13 at 05:43
  • do i have to change field name in login.ctp from email to 'username' ? – hellojohn Jun 15 '13 at 05:53