I ran my login.ctp....it gave me a Fatal error saying "Call to a member function allow() on a non-object" and i dont how to fix it...This is my AppController it had the error in the beforeFilter function:
public $component1 = array(
'Session', 'Auth' => array(
'loginRedirect' => array(
'controller' => 'users',
'action' => 'home'
),
'logoutRedirect' => array(
'controller' => 'users',
'action' => 'login'
)
)
);
function beforeFilter(){
parent::beforeFilter();
$this->Auth->allow('add','view');
you should changepublic $component1 to public $components
Related
I have used Auth component in my OrdersController as follows:
public $components = array(
'Session',
'Auth' => array(
'authenticate' => array(
'Form' => array(
'fields' => array(
'username' => 'email', //Default is 'username' in the userModel
'password' => 'password' //Default is 'password' in the userModel
),
'userModel' => 'Agent'
)
),
'loginAction' => array(
'controller' => 'admins',
'action' => 'login'
),
'loginRedirect' => array('controller' => 'admins', 'action' => 'deshboard'),
'logoutRedirect' => array('controller' => 'admins', 'action' => 'login'),
'authError' => "You can't acces that page",
'authorize' => 'Controller'
)
);
public function beforeFilter() {
parent::beforeFilter();
// Allow users to register and logout.
$this->Auth->allow('login','index');
}
When I tried to login and username and password matched it redirect to adminc/deshboard with the following error message:
$controller does not implement an isAuthorized() method.
Error: An Internal Error Has Occurred.
I searched google for couple of hours no solution. What am I doing for this error? Thanks for your time.
You need to implement isAuthorized(), like so:
class OrdersController extends Controller {
//...
public function isAuthorized($user) {
//auth check
//return boolean
}
//...
}
See http://book.cakephp.org/2.0/en/tutorials-and-examples/blog-auth-example/auth.html for more info.
Auth Flash params are not working for me.
AppController:
class AppController extends Controller {
public $helpers = array(
'Session',
'Facebook.Facebook',
'Html' => array('className' => 'BoostCake.BoostCakeHtml'),
'Form' => array('className' => 'BoostCake.BoostCakeForm'),
'Paginator' => array('className' => 'BoostCake.BoostCakePaginator'),
'AssetCompress.AssetCompress',
'PhpThumb.PhpThumb'
);
public $components = array(
'Session',
'RequestHandler',
'Auth' => array(
'authenticate' => array('Custom'),
'loginRedirect' => array('controller' => 'users', 'action' => 'index'),
'logoutRedirect' => array('controller' => 'pages', 'action' => 'index'),
'flash' => array(
'element' => 'alert',
'key' => 'auth',
'params' => array(
'plugin' => 'BoostCake',
'class' => 'alert-danger'
)
)
),
);
Default layout:
Session->flash();
echo $this->Session->flash('auth');
echo $this->fetch('content');
?>
I've debugged the params to make sure they're outputting correctly, but when the alert pops up this is what I see:
<div id="authMessage" class="message">Username or password is incorrect</div>
Thanks in advance
You need to create a ctp with the name alert.ctp in your view as follows:
<div id="authMessage" class="message">
<?php
echo $plugin;
echo $class;
?>
Username or password is incorrect
</div>
Have a look at this CakePHP custom flash for loginError
I am new to cakephp. I have a problom while login. With wrong name and password redirects to login home page.
UsersController.php
public function login() {
$this->layout = 'admin-login';
if ($this->request->is('post')) {
if ($this->Auth->login($this->request->data)) {
return $this->redirect($this->Auth->redirectUrl())
} else {
$this->Session->setFlash(__('Username or password is incorrect'), 'default', array(), 'auth');
}
}
}
AppController.php
public $components = array(
'Session',
'Auth' => array(
'loginRedirect' => array('controller' => 'users', 'action' => 'index'),
'logoutRedirect' => array('controller' => 'users', 'action' => 'login'),
'loginAction' => array('controller' => 'users', 'action' => 'login')
)
);
public function beforeFilter() {
$this->Auth->allow("login");
//$this->Auth->authorize = array('Controller');
$this->Auth->authenticate = array(
'Form' => array (
'scope' => array(
'User.is_active' => 1
)
)
);
}
public function isAuthorized($user) {
return true;
}
login.ctp
echo $this->Form->create('User');
echo $this->Form->input('username');
echo $this->Form->input('password');
echo $this->Form->submit(__('Submit');
echo $this->Form->end();
When i fill the wrong username & password & click on submit button it redirect to home page, Thanks.
You are using AuthComponent::login() wrong, you are only supposed to pass data to it in case you want to manually login a user, ie without automatic authentication.
If you want to use the components authentication functionality just call $this->Auth->login()
See also: http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#identifying-users-and-logging-them-in
In 2.x $this->Auth->login($this->request->data) will log the user in with whatever data is posted, whereas in 1.3 $this->Auth->login($this->data) would try to identify the user first and only log in when successful.
usercontroller
public function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow('login','logout');
}
public function login()
{
$this->layout= 'login';
if ($this->request->is('post')) {
if ($this->Auth->login()) {
$this->redirect('/users');
else {
$this->Session->setFlash(__('Invalid email or password, please try again'));
}
}
else{
if($this->Auth->loggedIn())
$this->redirect('index');
}
}
}
AppController
class AppController extends Controller {
public $components = array(
'Session',
'Auth' => array(
'authenticate' => array(
'Form' => array(
'userModel' => 'User',
'fields' => array(
'username' => 'user_name',
'password' => 'password'
)
)
),
'loginAction' => array('controller' => 'users', 'action' => 'login'),
'loginRedirect' => array('controller' => 'users', 'action' => 'dashboard'),
'logoutRedirect' => array('controller' => 'users', 'action' => 'login'),
'authError' => 'You don\'t have access here.',
/*
'loginAction' => array('controller' => 'users', 'action' => 'forgot_password'),
'loginRedirect' => array('controller' => 'users', 'action' => 'dashboard'),
'logoutRedirect' => array('controller' => 'users', 'action' => 'forgot_password'),
'authError' => 'You don\'t have access here.',
*/
),
);
Sorry - Hate to ask but I've spent hour's working this out and researching but havent had any luck.
CakePHP (running the latest version) seems to refuse to use the fields setting (So that I can use the email column in the database as the username). If I set it to 'email' which is the field I wish to use from the database it simply refuses to login stating incorrect details. Cant get any output from SQL in DebugKit for some reason. Although when it's set to username as per below it works fine just using a 'temp' column in the DB. I've tried putting it in the components var but had no luck with that either. What could I be doing wrong? Debug is on, cant see any errors in the log or browser.
The model does contain an email column.
Controller/AppController.php
class AppController extends Controller {
public $components = array(
'Session',
'DebugKit.Toolbar',
'Auth' => array(
'allow' => array('login','logout'),
'loginAction' => array('controller' => 'users', 'action' => 'login'),
'loginRedirect' => array('controller' => 'dashboard', 'action' => 'index'),
'authorize' => 'Controller'
)
);
function beforeFilter() {
Security::setHash('md5');
$this->Auth->authenticate = array(
'Form' => array(
'fields' => array(
'username' => 'username',
),
),
);
}
}
Controller/UserController.php
class UsersController extends AppController {
public $uses = array('User');
public function beforeFilter() {
parent::beforeFilter();
}
public function isAuthorized($user){
return true;
}
public function login() {
$this->layout = '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','flash_error');
}
}
}
public function logout() {
$this->layout = 'login';
$this->Session->setFlash('Successfully logged out!','flash_success');
$this->redirect($this->Auth->logout());
}
}
View/Users/login.ctp
<?php
$this->set('title_for_layout', 'Login');
echo $this->Session->flash();
echo $this->Session->flash('auth','flash_info');
echo $this->Form->create('User', array(
'action' => 'login'
));
echo $this->Form->input('username',array(
'between' => '<br/>',
'before' => '<p>',
'after' => '</p>',
'class' => 'text',
'label' => 'Email:'
));
echo $this->Form->input('password',array(
'between' => '<br/>',
'before' => '<p>',
'after' => '</p>',
'class' => 'text',
'label' => 'Password:'
));
echo $this->Form->submit('Login', array(
'class' => 'submit',
'before' => '<p>',
'after' => '</p>'
));
echo $this->Form->end();
?>
You need to change the name of the field on your form from username to email. Just setting the label to "email" is not enough.
echo $this->Form->input('email',array(
'between' => '<br/>',
'before' => '<p>',
'after' => '</p>',
'class' => 'text',
'label' => 'Email:'
Try updating the components code in your appController to add the authenticate values to the Auth array like this:
public $components = array(
'Session',
'DebugKit.Toolbar',
'Auth' => array(
'allow' => array('login','logout'),
'loginAction' => array('controller' => 'users', 'action' => 'login'),
'loginRedirect' => array('controller' => 'dashboard', 'action' => 'index'),
'authorize' => 'Controller',
'authenticate' => array(
'Form' => array(
'fields' => array('username' => 'email')
)
)
)
);
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().