I've done this many times and this problem never occured.

$this->Auth->user() returns an array only with username and password fields

$this->Auth->user('id') returns null.

Anyone?

EDIT

AuthComponent definition in AppController:

public $components = array(
    'Session',
    'Cookie',
    'Auth' => array(
        'authorize' => 'Controller',
        'loginError' => 'Invalid account specified',
        'authError' => 'No Permission',
        'logoutRedirect' => array('controller' => 'users', 'action' => 'login'),
    ),
);

EDIT 2

User model:

<?php

class User extends AppModel {

public $validate = array(
    '_password' => array(
        'equaltofield' => array(
            'rule' => array('equaltofield', 'password'),
            'message' => 'Require the same value to password.',
        )
    )
);

public function beforeValidate ($options = array()) {
    $this->data[$this->alias]['password'] = AuthComponent :: password($this->data[$this->alias]['password']);
    $this->data[$this->alias]['_password'] = AuthComponent :: password($this->data[$this->alias]['_password']);
}

function equaltofield($check,$otherfield) {
    $fname = '';
    foreach ($check as $key => $value){
        $fname = $key;
        break;
    }
    return $this->data[$this->name][$otherfield] === $this->data[$this->name][$fname];
} 

}

?>

UsersController login/logout functions:

public function login () {
    if($this->request->is('post')) {
        if($this->Auth->login($this->request->data)) {
            if($this->Auth->user('role' == 'administrator')) {
                return $this->redirect(array('controller' => 'brands', 'action' => 'index'));   
            }
            else {
                return $this->redirect(array('controller' => 'visits', 'action' => 'index'));   
            }
        }
        else {
            $this->Session->setFlash('Login incorrect');    
        }
    }
}

public function logout () {
    $this->Auth->logout();
    return $this->redirect(array('action' => 'login')); 
}
有帮助吗?

解决方案

Problem is on login action-

Remove $this->request->data from $this->Auth->login();

it must be as-

public function login () {
    if($this->request->is('post')) {
        if($this->Auth->login()) {
            if($this->Auth->user('role' == 'administrator')) {
                return $this->redirect(array('controller' => 'brands', 'action' => 'index'));   
            }
            else {
                return $this->redirect(array('controller' => 'visits', 'action' => 'index'));   
            }
        }
        else {
            $this->Session->setFlash('Login incorrect');    
        }
    }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top