When cake2.x performs the login process and the logged-in user tries to access the login action again, I want to get the user information of the logged-in user and redirect to another page if logged in, but it does not work.
To check whether you are logged in or not, there is a way to judge whether the information is in $this->Auth->user() by using $this->auth->loggedIn, so $this->Auth->user I wrote a process that uses () to get the login information and redirect it, but the logged-in user will be accessed. I would like to ask someone who can understand the cause.
Addendum
If you enter the controller and a cushion from the url that can be redirected when you go from the link to login, it will be accessed
class AppController extends Controller {
public $helpers = array('Html', 'Form', 'Flash', 'Session');
public $components = array(
'DebugKit.Toolbar',
'Flash',
'Session',
'Auth' => array(
'loginRedirect' => array(
'controller' => 'posts',
'action' => 'index'
),
'logoutRedirect' => array(
'controller' => 'posts',
'action' => 'index'
),
'authenticate' => array(
'Form' => array(
'passwordHasher' => 'Blowfish',
'fields' => array(
'username' => 'email',
'password' => 'password'
)
)
),
'loginAction' => array(
'controller' => 'users',
'action' => 'login'
),
'unauthorizedRedirect' => array(
'controller' => 'posts',
'action' => 'index'
),
'authorize' => array('Controller')
)
);
public function beforeFilter() {
$this->Auth->allow('index', 'view');
$this->set('auth', $this->Auth->user());
}
}
public function login() {
$login_id = $this->Auth->user('id');
if ($login_id) {
$this->Flash->error(__('You are logged in'));
return $this->redirect(array('controller' => 'posts', 'action' => 'index'));
}
if ($this->request->is('post')) {
if ($this->Auth->login()) {
$this->Flash->success(__('Login succeeded));
$this->redirect($this->Auth->redirectUrl());
} else {
$this->Flash->error(__('wrong email address or password'));
}
}