Thursday, 12 September 2013

loginRedirect in cakephp not working correctly

loginRedirect in cakephp not working correctly

In our cakephp application, we tried auth component for login.
This is the AppControllere:
class AppController extends Controller {
public $components = array(
'Session',
'Auth' => array(
'loginRedirect' => array('controller' => 'homes', 'action' =>
'dashboard'),
'logoutRedirect' => array('controller' => 'pages', 'action' =>
'home')
)
);
public function beforeFilter() {
$this->Auth->allow('index','logout','display','home');
}
This is the UsersController:
class UsersController extends AppController {
public function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow('add');
}
public function login() {
if ($this->request->is('post')) {
if ($this->Auth->login()) {
$this->redirect($this->Auth->redirect());
} else {
$this->Session->setFlash(__('Invalid username or password,
try again'));
}
}
}
public function logout() {
$this->redirect($this->Auth->logout());
}
public function index() {
$this->User->recursive = 0;
$this->set('users', $this->paginate());
}
public function add() {
if ($this->request->is('post')) {
$this->User->create();
if ($this->User->save($this->request->data)) {
$this->Session->setFlash(__('Checkin now'));
$this->redirect(array('action' => 'login'));
} else {
$this->Session->setFlash(__('The user could not be saved.
Please, try again.'));
}
}
}
}
The the following the steps:
we took the url in browser as http://localhost/cakephp/
Then there have a link in home.ctp page for login, clicked on that go to
http://localhost/cakephp/users/login/
username and password entered and clicked the login button
then it goes to the same home.ctp page not the page we mentioned in
AppController
Second attempt:
we took the direct url as http://localhost/cakephp/users/login/
then entered login details , then it will go the correct page.
So what will happen here, Why auth componet behave like this.....

No comments:

Post a Comment