When i follow $this->Auth->allow('login'); this approach, we are not able to redirect to dashboard page. If i follow $this->Auth->allow(); this approach we are able to redirect to dashboard page.
But i need to use first approach for login.
I have a AppController.php which is performed Login Action.
Below is the AppController.php Code
function beforeFilter() {
Security::setHash('md5');
// Authenticate
$this->Auth->authorize = 'Controller';
$this->Auth->autoRedirect = false;
$subdomain = substr( env("HTTP_HOST"), 0, strpos(env("HTTP_HOST"), ".") );
if($this->params['controller']=="moderator")
{
//$this->Auth->allow(); // if i use this one my code is working fine.
$this->Auth->allow('login');
$this->Auth->loginAction = array('controller' => 'moderator', 'action' => 'login');
$this->Auth->loginRedirect = array('controller' => 'moderator', 'action' => 'dashboard');
$this->Auth->loginError = 'No username and password was found with that combination.';
$this->Auth->logoutRedirect = array('controller' => 'moderator', 'action' => 'login');
AuthComponent::$sessionKey = 'Auth.Admin';
}
}
when we are login the page it will goto ModeratorController.php controller page and then check the usename and password using Auth , if username & password correct it will call the dashboard function.
Below is the ModeratorController.php Code
class ModeratorController extends AppController {
public $components = array('RequestHandler','Session',
'Auth'=>array(
'authenticate' => array(
'Form' => array(
'fields'=>array(
'username'=>'username',
'password'=>'password'
),
'userModel'=> 'Admin'
),
),
'loginAction' =>array(
'Controller' => 'moderator',
'action' => 'login'
),
'loginRedirect'=>array('Controller'=>'moderator', 'action'=>'index'),
'logoutRedirect'=>array('Controller'=>'moderator', 'action'=>'index'),
'authError'=>"You can't access that page",
'authorize'=>'Controller',
'loginError'=> 'Login error',
)
);
public $helpers = array('Js' => array('Jquery'),'Html','Paginator');
function beforeFilter() {
//used version cake 2.3.0.
parent::beforeFilter();
}
// for moderator login process
public function login() {
if($this->Session->read('Auth.Admin.admin_id'))
{
$this->redirect('dashboard');
}
if($this->request->data)
{
$this->Admin->set($this->data);
if($this->Admin->validates())
{
if($this->Auth->login()){
//$this->redirect('/moderator/dashboard'); // result: redirect to moderator/login
//$this->redirect(array('controller' => 'moderator', 'action' => 'dashboard')); // result: redirect to moderator/login
//$this->redirect($this->Auth->redirect()); // result: redirect to moderator/login
//$this->redirect(array('action' => 'dashboard')) // result: redirect to moderator/login
//$this->redirect('dashboard12'); // result: redirect to moderator/dashboard12 and give me 404. That result is correct
//$this->redirect('/dashboard'); // result: redirect to /dashboard and give me 404. That result is also correct
$this->redirect('dashboard')) // result: redirect to moderator/login
}
else
{
$this->Session->setFlash(__('Invalid email or password, try again'));
}
}
}
}
public function dashboard()
{
echo "I am in dashboard";
exit();
}
}
Thanks
I see a few things that are technically wrong:
When building in CakePHP you should follow its standards
You should have moderators as your controller and not moderator
You make reference to dashboard.php
Is that a controller? or are you referring to the method
If you have a controller name ModeratorsController.php and a method there name dashboard(), then you can redirect like this:
$this->redirect(array('controller' => 'moderators', 'action' => 'dashboard'));
or you could even redirect like this
$this->redirect('/moderators/dashboard');
If the dashboard.php file you make reference is actually the controller, which in this case should be DashboardsControler.php, then you have to redirect to
$this->redirect('controller' => 'dashboards');
or
$this->redirect('/dashboards');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With