0
votes

I need to edit the loginredirect session parameter here so that it will redirect to different page as an admin or user... thing is. with index hard coded in, it will always redirect to the normal user index view ignoring my code to redirect admin users . I want it so that if the current user is an admin, it will redirect to admin_index instead

appControler

public $components = array(
        'Session',
        'Auth' => array(
            'loginRedirect' => array(
                'controller' => 'users',
                'action' => 'index'
            ),
            'logoutRedirect' => array(
                'controller' => 'users',
                'action' => 'login'
                ),
             'authenticate' => array(
                'Form' => array(
                    'passwordHasher' => 'Blowfish'
                )
            )
        )

    );

user controller login function

public function login() {
    if($this->Auth->user('account_type')=='admin'){
        return $this->Auth->loginRedirect = array('controller' => 'users', 
        'action' => 'admin_index');
    }
    elseif($this->Auth->user('account_type')=='user'){
        return $this->Auth->loginRedirect = array('controller' => 'users',
        'action' => 'view');
    }
    else {
        if ($this->Auth->login()) {
            return $this->redirect($this->Auth->redirect());
        }
        $this->Session->setFlash(__('Invalid username or password, try again'));
    }
}
3
First check if user login if($this->Auth->login()) { REST OF YOUR CODE } - Salines
added said line... now all my logins always redirect to the login page but the login link on top of the page is now logout means my user is indeed logged in - Sean Perez

3 Answers

0
votes

Change return $this->redirect($this->Auth->redirect()); to

return $this->redirect($this->Auth->redirectUrl());

Your problem is that you return the url for admins but never really redirect.

Alternatively you could add the desired url to $this->Auth->redirectUrl() in a string or an array format

1
votes

From CakePHP documentation, about $loginRedirect: http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#AuthComponent::$loginRedirect

This value will be ignored if the user has an Auth.redirect value in their session.

So your $this->Auth->loginRedirect is being ignored. You can move this logic to your beforeFilter() callback and set $this->Auth->loginRedirect there, or you can redirect the user manually.

public function login() {
    if($this->Auth->user('account_type')=='admin'){
       return $this->redirect(array('controller' => 'users', 'action' => 'admin_index'));
    }
    elseif($this->Auth->user('account_type')=='user'){
        return $this->redirect(array('controller' => 'users', 'action' => 'view'));
    }
    else {
        if ($this->Auth->login()) {
            return $this->redirect($this->Auth->redirect());
        }
        $this->Session->setFlash(__('Invalid username or password, try again'));
    }
}
0
votes

I was getting the same issues in case of wrong authentication it was redirecting to wrong URL. So i try the below code for login function and it work for me.

$user = $this->Users->newEntity();
    $this->set('user', $user);

    if ($this->request->is('post')) {
        $user = $this->Auth->identify();
        if ($user){
            $this->Auth->setUser($user);
            return $this->redirect($this->Auth->redirectUrl());
        }else{
            $this->Flash->error(__('Invalid username or password, try again')); 
        }
    }