Auth logout is not working in CakePHP 2.x - cakephp

When I login from one user account session is set.Then opening the next tab on same browser and enter login url it takes me to the login page.But actually it should redirect to the "dashboard" page(in my case). It can't redirect to loginRedirect(dashboard) page as mentioned in my Auth.
When i logout, as per my code session,cookie and cache are deleted. but it's not redirect to logoutRedirect page.
My code :
App controller
public $components = array(
'Session', 'RequestHandler', 'Email', 'Cookie',
'Auth' => array(
'authenticate' => array(
'Form' => array(
'fields' => array(
'username' => 'email',
'password' => 'password')
)
),
'loginRedirect' => array(
'controller' => 'users',
'action' => 'login'
),
'logoutRedirect' => array(
'controller' => 'users',
'action' => 'login'
)
)
);
User controller
login action :
public function login() {
$this->layout = 'admin';
if ($this->Session->check('Auth.User')) {
$this->redirect(array('controller' => 'users', 'action' => 'dashboard'));
}
if (isset($this->data['User'])) {
if (!empty($this->data['User']['email']) && !empty($this->data['User']['password'])) {
if ($this->Auth->login()) {
$this->redirect(array('controller' => 'users', 'action' => 'dashboard'));
} else {
$this->set('error', "Email or Password mismatch.");
}
}
} else {
if ($this->Auth->loggedIn()) {
$this->redirect(array('controller' => 'users', 'action' => 'dashboard'));
}
}
}
logout action :
public function logout() {
header('pragma: no-cache');
header('Cache-Control: no-cache, must-revalidate');
$this->response->disableCache();
$this->Session->delete('Auth.User');
$this->Session->delete('User');
$this->Session->destroy();
$this->Cookie->destroy();
return $this->redirect($this->Auth->logout());
}
This code is working fine in "local server" but not working in production server.

Your redirect statements should have return in front of them so that code execution will stop there. For example:
return $this->redirect(array('controller' => 'users', 'action' => 'dashboard'));

Related

cake php Auth login not working online but working locally

i am using cakephp Auth login it is not working online but on localhost it is working fine.
class AppController extends Controller {
public $components=array(
'Auth'=>array(
'authenticate'=>array(
'Form'=>array(
'userModel'=>'User',
'fields'=>array(
'username'=>'uid',
'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.',
),
'Session'
);
this login() method in UsersController this is working fine at localhost what changes should be done when we are uploading any file on server in cakephp
public function login(){
$this->layout='login';
if($this->request->is('Post'))
{
if($this->Auth->login())
{
$user=$this->Auth->user();
if($user['status']!=0 || $user['role']=='1')
{
if($user['role'])
{
$role="Admin";
$this->Session->write('admin',$user['uid']);
}
else
{
$role="User";
$this->Session->write('user',$user['uid']);
}
$this->Session->write('rolename',$role);
$this->Session->write('uid',$user['uid']);
$log['Log']['loginType']=$this->request->data['User']['loginType'];
$log['Log']['userId']=$user['id'];
$log['Log']['latitude']='0';//$data['User']['latitude'];
$log['Log']['longitude']='0';//$data['User']['longitude'];
$this->Log->save($log);
$this->Flash->set(_('Sucessfully login'));
$this->redirect(array('controller'=>'users','action'=>'dashboard'));
}
else
{
$this->Flash->set('You have no access to login...');
}
}
else
$this->Flash->set('Invalid Credentials');
}
}

Login Script in 2.4.2 is not working

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.',
*/
),
);

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

Can't seem to get CakePHP's AuthComponent working

I'm completely lost in trying to setup the AuthComponent.
Every login fails.
Here's my AppController beforeFilter function:
public function beforeFilter() {
$this->Auth->authenticate = array(
'all' => array(
'userModel' => 'ClientUser',
'fields' => array(
'username' => 'login',
'password' => 'password'
)
)
);
$this->Auth->loginAction = array('controller' => 'client_users', 'action' => 'login');
$this->Auth->loginRedirect = array('controller' => 'static', 'action' => 'clientcenter');
$this->Auth->logoutRedirect = array('controller' => 'static', 'action' => 'home');
// I deny stuff later on
$this->Auth->allow();
}
And here's the login function in the ClientUsers controller:
public function login() {
// Check login data
if ($this->Auth->login()) {
return $this->redirect($this->Auth->redirect());
} else {
$this->Session->setFlash(__('Username or password is incorrect'), 'default', array(), 'auth');
}
}
And it always fails. And I have no idea why.
This is my $request->data content: (I've actually used "login" and "username" as field name, none work)
ClientUser
login: user#email.com
password: thepassword
Client passwords are hashed in the Model, using the authcomponent (which is imported at the top of the script. I used the security hash function earlier, but that also did not work):
public function beforeSave($options) {
$this->data['ClientUser']['password'] = AuthComponent::password($this->data['ClientUser']['password1']);
return true;
}
Where is you Auth adapter?
as explained on http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#authentication-objects
// at least one adapter is necessary (here Form)
public $components = array(
'Auth' => array(
'authenticate' => array(
'Form' => array(
'fields' => array('username' => 'email')
)
)
)
);

redirect upon login

I want users to be redirected to the posts index action upon login. But they instead get redirected to posts's add action.
below is my routes.php
Router::connect('/', array('controller' => 'posts', 'action' => 'index'));
/**
* ...and connect the rest of 'Pages' controller's urls.
*/
Router::connect('/pages/*', array('controller' => 'posts', 'action' => 'index'));
and my login action in users controller is
public function login() {
if ($this->request->is('post')) {
if ($this->Auth->login()) {
$this->redirect($this->Auth->redirect());
} else {
$this->Session->setFlash('Your username or password was incorrect.');
}
}
}
** DEBUG **
when I did debug upon login, I get this
/app/Controller/UsersController.php (line 20)
object(AuthComponent) {
components => array(
(int) 0 => 'Session',
(int) 1 => 'RequestHandler'
)
authenticate => array(
(int) 0 => 'Form'
)
authorize => array(
'Actions' => array(
'actionPath' => 'controllers'
)
)
ajaxLogin => null
flash => array(
'element' => 'default',
'key' => 'auth',
'params' => array()
)
loginAction => array(
'controller' => 'users',
'action' => 'login'
)
loginRedirect => array(
'controller' => 'posts',
'action' => 'add'
)
logoutRedirect => array(
'controller' => 'users',
'action' => 'login'
)
for some reason it says
loginRedirect => array(
'controller' => 'posts',
'action' => 'add'
)
instead of action => index it is unsing action => add. Whys that
Auth->redirect() returns the url where the user landed before being taken to the login page or Auth->loginRedirect.
https://stackoverflow.com/a/2636940/643500
So, if you set the root path to the index it might do the trick.

Resources