Question

I am trying to implement remember me functionality in cakephp.First time when user hits the domain login page should appear, if user clicks the remember me check box while login, when next time user hits the domain it should redirect to another page not the login page, if remember me is not checked then login page should appear.

Here remember me is working, but if user is logged in in need to display the userhome page not the login page

how can i implement this.

Thanks for the help...

Here is my code i am working on:

routes.php:

Router::connect('/', array('controller' => 'users', 'action'=>'login'));
Router::connect('/:action', 
array('controller' => 'users', 'action' =>    'userhome'),
array('pass' => array('')));

AppController.php

public $components = array(
'Session','Auth' => array('loginRedirect' => 
array('controller' => 'users', 'action' => 'userhome'),
'logoutRedirect' => '/','authorize'=>array('Controller'),),'Cookie');


public function beforeFilter() {
$this->Cookie->httpOnly = true;
if (!$this->Auth->loggedIn() && $this->Cookie->read('rememberMe')) {
        $cookie = $this->Cookie->read('rememberMe');
        echo debug($cookie);
        $this->loadModel('User'); // If the User model is not loaded already
        $user = $this->User->find('first', array(
                'conditions' => array(
                        'User.username' => $cookie['username'],
                        'User.password' => $cookie['password']
                )
        ));

public function login() {
if($this->request->is('post')){

if ($this->Auth->login())  {

    if ($this->request->data['User']['rememberMe'] == 1) {
                        // After what time frame should the cookie expire
    $cookieTime = "12 months"; // You can do e.g: 1 week, 17 weeks, 14 days

    // remove "remember me checkbox"
    unset($this->request->data['User']['rememberMe']);

    // 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);
}



$this->redirect($this->Auth->redirect());


}


}




}

        if ($user && !$this->Auth->login($user['User'])) {
            $this->redirect('/users/logout'); // destroy session &   cookie
        }
    }

View:

<?php echo $this->Form->input('rememberMe', array('type' => 'checkbox',
'label' => 'Remember me','class'=>'rememberme')); ?>
Was it helpful?

Solution

I don't really understand your nested function login(), but I assume it is just sample from your /users/login action, so let me rewrite the beforeFilter method to redirect your user to /users/userhome if he hits your domain and is logged in using cookie:

public function beforeFilter() {
    $this->Cookie->httpOnly = true;
    if (!$this->Auth->loggedIn() && $this->Cookie->read('rememberMe')) {
            $cookie = $this->Cookie->read('rememberMe');
            debug($cookie); // no need to echo it, debug function does that already
            $this->loadModel('User'); // If the User model is not loaded already
            $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('/users/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 /
        }
    }
}

FYI: $this->Auth->redirect() is deprecated as of 2.3, use $this->Auth->redirectUrl() instead

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top