How redirect home page in cake php - cakephp

In my cake php site, after login page redirect to result display page, but I need to redirect home page. Here is AppController.php code:
class AppController extends Controller {
public $components = array(
'Session',
'Auth' => array(
'loginRedirect' => array('controller' => 'results', 'action' => 'add'),
'logoutRedirect' => array('controller' => 'users', 'action' => 'login')
),
'Security'
);
}

Just define loginRedirect
To force where a user is redirected to after login, just change the loginRedirect:
class AppController extends Controller {
public $components = array(
'Session',
'Auth' => array(
'loginRedirect' => '/', # <-
'logoutRedirect' => array('controller' => 'users', 'action' => 'login')
),
'Security'
);
}
Note that ordinarily a user is redirected to whatever url they were attempting to access - with a default of /; as such it may be more appropriate to simply remove the loginRedirect key from the Auth component config.

Related

Admin login redirect in cakephp

hi everyone back at coding. stuck with a strange error. the auth component on cakephp logs the user in and is also redirecting the user to the dashboard page but the error is that when the user lands on the dashboard the login form is still visible and the actual content of the dashboard is not showing.
//AppController.php
// Pass settings in $components array
public $components = array(
'Session',
'Auth' => array(
'loginAction' => array(
'controller' => 'Admins',
'action' => 'Dashboard',
///'plugin' => 'users'
),
'logoutRedirect' => array(
'controller' => 'Admins',
'action' => 'Login',
),
'authError' => 'Enter correct admin username and password',
'authenticate' => array(
'Form' => array(
'fields' => array('username' => 'admin#test.com',
'password' => 'pass')
)
)
)
);
public function beforeFilter(){
//non-logged in users
$allowed = array('Login', 'Logout');
$this->Auth->allow($allowed);
$user = null;
if($this->Session->read('user')){
$user = $this->Session->read('user');
$this->user = $user;
$this->set('user', $user);
$this->redirect(array('controller'=>'Admins', 'action'=>'Dashboard'));
}
}
i'm not storing the admin login information in table since only one admin is required.
//Login function in AdminsController.php
public function Login(){
if($this->request->is('post')){
if($this->data['Admins']['username']=='admin#test.com' && $this->data['Admins']['password'] == 'pass'){
$this->Auth->login();
$this->Session->write($this->user, $this->data['Admins']['username']);
$this->Session->setFlash('Welcome Back !');
$this->redirect(array('controller'=>'Admins', 'action'=>'Dashboard'));
}
}
}
public function Dashboard(){
//pr($this->Session->read($user));
//pr($this->user);
//exit;
}
kindly ask for more information if needed, the problem is in the display for the ctp file, it displays the login.ctp rather than dashboard.ctp

How to change cakephp auth component controller?

Here my controller name is AucUsersController. After use auth component it is finding userscontroller.I want to change this directory.I have tried by bellow code but it's not working.
public $components = array('Paginator'=>array('limit'=>2),'Auth'=>array(
'Controller'=>'AucUsers',
'loginRedirect' => array('controller' => 'aucusers','action' => 'index'),
'logoutRedirect' => array('controller' => 'aucusers','action' => 'index'),
'authError'=>'You can not access this page!!',
));
How can I change this default controller ?
CakePHP by default uses users/login for loginAction,
loginAction is the property where you define the controller and action where cake does the login
public $components = array('Paginator'=>array('limit'=>2),'Auth'=>array(
'loginAction' => array(
'controller' => 'aucusers',
'action' => 'login'
),
'loginRedirect' => array('controller' => 'aucusers','action' => 'index'),
'logoutRedirect' => array('controller' => 'aucusers','action' => 'index'),
'authError'=>'You can not access this page!!',
));
loginRedirect - It represents where user should redirect to after login
logoutRedirect - It represents where user should redirect to after logout
I believe that if you want to change the default controller you have to set the UserModel option. I set it at beforeFilter method. So in your case it will be.
/**
* beforeFilter method
*
* #return void
*/
public function beforeFilter() {
$this->Auth->authenticate = array(
'Form' => array(
'userModel' => 'AucUser',
)
);
return parent::beforeFilter();
}
I haven't seen in the docs to have any controller option.

login not redirecting in cakephp

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'));
}
}
}
this is my login script and
public $components = array('Acl', 'Session',
'Auth' => array('authorize' => array('Controller'),
'loginRedirect' => array('controller' => 'users', 'action' => 'dashboard'),
'logoutRedirect' => array('controller' => 'users', 'action' => 'login'),
'authenticate' => array('Form' => array('fields' => array('username' => 'email')))
)
);
this is auth compnents in appcontroller.php
it is logging in using email and password but it is not redirecting to user/dashboard
but instead of that if i put any external urls it redirects perfectly
eg: 'loginRedirect' => 'http://google.com',
it redirects to google
i am totally lost.kindly help
Make sure you are allowed to view the dashboard page using: AuthComponent::allow():
Add this method to your controller:
public function beforeFilter() {
$this->Auth->allow('dashboard');
}
Make sure there is a route set for the dashboard page

How to allow actions in CakePHP?

I make something horribly wrong in Cake 2.3.6 stable. I followed the Auth tutorial and added in the AppController:
public function beforeFilter() {
$this->Auth->allow('index', 'view');
}
But when I enter the site on the homepage, cake throws that I´m not authorized to access that location.
With no effect I tried in the PagesController:
public function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow('index');
}
I double checked the tutorial and my code, there are no differences except I had to swap "$this->Post" with "$this->Calclulation" in CalculationsController.
Furthermore the login- and logout-redirects in AppController doesn´t work.
public $components = array(
'Session',
'Auth' => array(
'loginRedirect' => array('controller' => 'calculations', 'action' => 'index'),
'logoutRedirect' => array('controller' => 'pages', 'action' => ''),
'authorize' => array('Controller')
)
);
How could it be solved? Thanks in advance :)
Please, check default routers. app/Config/routes.php
Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));
this mean, that home page is actulay rendered by pages controller and action display,
so, you should allow display
$this->Auth->allow('display');

cakephp: authen function auto direct to user/login

i am beginning with cakephp framework, i use auth to create a login form, at my appcontroller i add:
class AppController extends Controller {
public $components = array('Auth', 'Cookie');
public function beforeFilter(){
$this->Auth->authenticate = array(
'Form' => array(
'userModel' => 'User',
'fields' => array('name' => 'name', 'password' => 'password'),
)
);
$this->Auth->loginAction = array('controller' => 'TestOnlineSystem', 'action' => 'P001');
$this->Auth->loginRedirect = array('controller' => 'TestOnlineSystem', 'action' => 'index');
$this->Auth->loginError = 'Failed to login';
$this->Auth->authError = ' ';
}
}
but when i run TestOnlineSystem/P001 it auto redirect to users/login anh show message net controller usercontroller. How can i fix it, P001 is my login page
I agree with thaJeztah, try the standard config from http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html :
public $components = array(
'Auth' => array(
'loginAction' => array(
'controller' => 'TestOnlineSystem',
'action' => 'P001',
'plugin' => 'users'
)
)
);
and get rid of your beforeFilter().

Resources