Reputation: 1107
Some people might find this question silly . But i really hav done all the googling, reading the cakephp documentation but still not able to understand about the Authentication mechanism of cakephp. I have tried my bit of code but still not able to authenticate....
My error for every proper entry i am getting error as invalid username-password. Here's my code
//login.tpl
<div class="users form">
<?php echo $this->Session->flash('auth'); ?>
<?php echo $this->Form->create('User'); ?>
<fieldset>
<legend><?php echo __('Please enter your username and password'); ?></legend>
<?php echo $this->Form->input('username');
echo $this->Form->input('password');
?>
</fieldset>
<?php echo $this->Form->end(__('Login')); ?>
</div>
//Controllers file
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'));
}
}
}
Can anyone please tell me how to authenticate, i am new to cakephp
Upvotes: 1
Views: 752
Reputation: 27382
Well cakephp handle authentication itself you don't need to write code inside login
function.
app_controller.php
<?php
class AppController extends Controller
{
var $components = array
(
'Auth',
'Session',
'RequestHandler',
'Email'
);
var $helpers = array
(
'Javascript',
'Form',
'Html',
'Session'
);
function beforeFilter()
{
$this->Auth->autoRedirect = true;
$this->Auth->authError = 'Sorry, you are not authorized to view that page.';
$this->Auth->loginError = 'invalid username and password combination.';
$this->Auth->loginAction = array
(
'controller' => 'users',
'action' => 'login',
'admin' => true
);
$this->Auth->logoutRedirect = array
(
'controller' => 'users',
'action' => 'logout',
'admin' => true
);
$this->Auth->loginRedirect = array
(
'controller' => 'users',
'action' => 'dashboard',
'admin' => true
);
$this->Auth->fields = array('username' => 'email', 'password' => 'password');
}
?>
users_controller.php
<?php
class UsersController extends AppController
{
var $name = 'Users';
/*
do not forget to add beforeFilter function and inside this function call parent beforeFilter function.
*/
function beforeFilter()
{
parent::beforeFilter();
}
function admin_login()
{
}
function admin_logout()
{
$this->Session->destroy();
$this->Auth->logout();
$this->Session->setFlash(__('Yor are now Logged out Successfully', true), 'default',array('class'=>'alert alert-success'));
$this->redirect('/');
exit;
}
?>
And you are done.
Upvotes: 1