I am implementing Ajax login using auth component in my cakephp app. Everything is working fine except remember me.
I am trying to set cookie using Cookie component but seems it's not sending cookie with response.
I have tried other setting domain, path and user agent check false in session variable but it didn't work.
If I use setcookie
method then it's sending cookie in response (but I need cakephp cookie as I am saving array in cookie)
Below is code that I am using:
public function beforeFilter() {
parent::beforeFilter();
$this->Cookie->path = '/';
$this->Cookie->httpOnly = true;
if (!$this->Auth->loggedIn() && $this->Cookie->read('rememberMe')) {
$cookie = $this->Cookie->read('rememberMe');
$user = $this->User->find('first', array(
'conditions' => array(
'User.username' => $cookie['username'],
'User.password' => $cookie['password']
)
));
if ($user && !$this->Auth->login($user['User'])) {
$this->redirect(array('action' => 'logout')); // destroy session & cookie
} else {
$this->redirect($this->Auth->redirectUrl()); // redirect to Auth.redirect if it is set, else to Auth.loginRedirect ('/users/userhome') if it is set, else to /
}
}
}
Here is login function code:
if ($this->Auth->login()) {
Croogo::dispatchEvent('Controller.Users.loginSuccessful', $this);
if ($this->request->data['User']['remember_me'] == 1) {
$cookieTime = "2 months"; // You can do e.g: 1 week, 17 weeks, 14 days
// remove "remember me checkbox"
unset($this->request->data['User']['remember_me']);
// hash the user's password
$this->request->data['User']['password'] = $this->Auth->password($this->request->data['User']['password']);
// write the cookie
$this->Cookie->write('rememberMe', $this->request->data['User'], true, $cookieTime);
}
$response['status'] = 'success';
$response['redirect_url'] = Router::url(array('action' => 'dashboard'), true);
$response['action'] = 'login';
$response['message'] = __d('sharbook', 'You have logged in successfully. Please stand by...');
echo json_encode($response);
}
Please help me to fix issue.