Cakephp Auth component does not work correctly

2

I am making a user registration screen. When the user completes the registration, he is redirected to a restricted area. In this same screen, there is a login and password field for the logs themselves. However, the only thing that works is the registration, but even so, it is not redirecting to the restricted area. My code looks like this:

AppController:

class AppController extends Controller {

public $components = array('Auth');

function beforeFilter() {
    $this->Auth->fields = array(
        'username' => 'username',
        'password' => 'password'
    );

    $this->Auth->loginAction = array(
        'controller' => 'users',
        'action' => 'login'
    );
    $this->Auth->loginRedirect = array(
        'controller' => 'profiles',
        'action' => 'index'
    );
    $this->Auth->logoutRedirect = '/';

    $this->Auth->authError = 'Area Restrita! Efetue login!'; // Mensagem ao entrar em area restrita
    $this->Auth->loginError = 'Nome de usuario ou senha não conferem!'; // Mensagem quando não se autenticar
    $this->Auth->allow('pages', 'display');

}

}

UsersController:

class UsersController extends AppController {

public function register() {
    $this->loadModel('User');
    //$this->autoRender = FALSE;
    if ($this->request->is('post')) {
        $day = $this->request->data['day'];
        $mouth = $this->request->data['mouth'];
        $year = $this->request->data['year'];
        $birth = $year . "-" . $mouth . "-" . $day;
        $date = date('Y-m-d H:i:s');

        if ($this->User->save(array(
                    'name' => $this->request->data['name'],
                    'username' => $this->request->data['email1'],
                    'password' => $this->request->data['pass'],
                    'birthday' => $birth,
                    'sex' => $this->request->data['sex'],
                    'date_sign' => $date
                ))) {
            $this->Auth->loginRedirect(array('controller' => 'profiles', 'action' => 'index'));              
        }
    }
}

public function login() {
    //$this->redirect($this->Auth->redirect());   
    if ($this->Auth->login()) {
        $this->redirect($this->Auth->redirect());
    } else {
        $this->Session->setFlash(__('Usuário ou senha inválido'));
    }
}

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

}

User (Model):

App::uses('AuthComponent', 'Controller/Component');

class User extends AppModel {

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;
}

}

And in the default.ctp layout, the form that makes the registration is pointing to "users / register" and the form for the login, pointing to "users / login"

Can anyone tell me what might be wrong?

    
asked by anonymous 05.03.2014 / 19:00

1 answer

1

Try this:

if ($this->User->save(array(
                'name' => $this->request->data['name'],
                'username' => $this->request->data['email1'],
                'password' => $this->request->data['pass'],
                'birthday' => $birth,
                'sex' => $this->request->data['sex'],
                'date_sign' => $date
            ))) {
        $this->Session->setFlash('O usuários foi salvo.');
        $this->redirect(array('controller' => 'profiles', 'action' => 'index'));              
    } else {
        $this->Session->setFlash('Erro no cadastro.');
}

For controller redirects always use $ this-> redirect ().

Login function in the UsersController:

public function login() {
//Verifica o tipo de requisição.
if ($this->request->is('post')) {  
    if ($this->Auth->login()) {
        $this->redirect($this->Auth->redirect());
    } else {
        $this->Session->setFlash(__('Usuário ou senha inválido'));
    }
}

}

    
05.03.2014 / 19:08