CakePHP Help
I decided to add this story to answer any questions that come up about CakePHP from my site. This one was inspired by mariusg who posted a question about CakePHP to the site.
mariusg: How did you do the "remember me thing" when you try to login. I mean, the code that integrates with cakephp.
nlofaso: That function is created by using cookies, first changing the login function to include the capability for writing the cookies, I changed what my cookie names are, but here's what the login function looks like in my users controller, the keys are to add a remember me check box to the users login form, then in the controller, check for the checkbox to be selected in the controller, and set the cookie accordingly:
function login() {
if ($this->Auth->User()){
if (!empty($this->data)){
if (!($this->data['User']['remember'])){
$this->Cookie->del('User');
} else {
$cookie = array();
$cookie['account'] = $this->data['User']['account'];
$cookie['pass'] = $this->data['User']['pass'];
$this->Cookie->write('User',$cookie,true,'+2 weeks');
}
unset($this->data['User']['remember']);
}
$this->redirect($this->Auth->redirect());
exit();
}
}
I also added to the app_controller the code to check for a valid cookie for the site. I read the cookie and if there isn't a valid user logged in, I call the function in my model to check for a valid user:
$cookie = $this->Cookie->read('User');
if (is_array($cookie) && !$this->Auth->user())
{
$loginUser = $this->User->checkLogin($cookie['account'], $cookie['pass']);
if (isset($loginUser)){
$loginSuccess = $this->Auth->login($loginUser['User']['id']);
if (!$loginSuccess){
$this->Cookie->del('User');
}
}
}
In the User model, I added the following function to see if there is a user who has the same data as found in the cookie created. I find the record in the DB that contains the users account and once I find it, I compare it to the password stored by the cookie, for an additional layer of security:
function checkLogin($account, $passhash)
{
$user = $this->findByAccount($account);
if ($user)
{
if ($user['User']['pass'] == $passhash)
return $user;
}
return null;
}
Thats basically how you add the "Remember Me" check box to a login page.
function login() {
if ($this->Auth->User()){
if (!empty($this->data)){
if (!($this->data['User']['remember'])){
$this->Cookie->del('User');
} else {
$cookie = array();
$cookie['account'] = $this->data['User']['account'];
$cookie['pass'] = $this->data['User']['pass'];
$this->Cookie->write('User',$cookie,true,'+2 weeks');
}
unset($this->data['User']['remember']);
}
$this->redirect($this->Auth->redirect());
exit();
}
}
I also added to the app_controller the code to check for a valid cookie for the site. I read the cookie and if there isn't a valid user logged in, I call the function in my model to check for a valid user:
$cookie = $this->Cookie->read('User');
if (is_array($cookie) && !$this->Auth->user())
{
$loginUser = $this->User->checkLogin($cookie['account'], $cookie['pass']);
if (isset($loginUser)){
$loginSuccess = $this->Auth->login($loginUser['User']['id']);
if (!$loginSuccess){
$this->Cookie->del('User');
}
}
}
In the User model, I added the following function to see if there is a user who has the same data as found in the cookie created. I find the record in the DB that contains the users account and once I find it, I compare it to the password stored by the cookie, for an additional layer of security:
function checkLogin($account, $passhash)
{
$user = $this->findByAccount($account);
if ($user)
{
if ($user['User']['pass'] == $passhash)
return $user;
}
return null;
}
Thats basically how you add the "Remember Me" check box to a login page.