CakePHP remember me with Auth

后端 未结 8 1899
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-30 06:22

I have successfully used Auth, but unfortunately, it seems that it does work only with Session. I want that if user checks \"Remember Me\" checkbox, I would use Cookie and h

相关标签:
8条回答
  • 2020-11-30 06:35

    Remember me is nothing else but session identified with a cookie, but cookie lifetime set to infinity. Look at Config/core.php for session cookie lifetime.

    0 讨论(0)
  • 2020-11-30 06:35

    you can try this

    if ($this->Auth->login()) 
            {
                if (!empty($this->data['User']['remember']))
                {
                    $cookie = array();
                    $cookie['login'] = $this->data['User']['login'];
                    $cookie['password'] = $this->data['User']['password'];
                                        $cookie['language'] =$this->data['User']['language'];
                    $this->Cookie->write('Auth.projectname', $cookie, true, '+1 years');                                                        
                    unset($this->data['User']['remember']);                                        
    
    0 讨论(0)
  • 2020-11-30 06:40

    It's been a while since the question was answered but hopefully this can help to ones that come after me.

    I've written short walkthrough on how to setup 'remember me' functionality using Auhenticate Plugin from Ceeram

    More info here: http://mirkoborivojevic.com/posts/2013/08/10/setup-remember-me-functionality-in-cakephp/

    0 讨论(0)
  • 2020-11-30 06:42
     public function admin_login() {
            $this->layout = 'admin_login';
            if (count($this->Session->read("Auth.User"))) {
                $usr = $this->Session->read("Auth.User");
                if ($usr['role'] == 'A' || $usr['role'] == 'RA' || $usr['role'] == 'MAfA' || $usr['role'] == 'Af' || $usr['role'] == 'FAA')
                    return $this->redirect(array('controller' => 'dashboard', 'action' => 'view'));
            }
            if ($this->request->is('post')) {
    
                if ($this->request->data['User']['remember_me']=="1") {
    //                pr($this->request->data);
    //                die('sdd');
    
    
                    $this->Cookie->write('username', $this->request->data['User']['username'], true, '1 year');
                    $this->Cookie->write('password', $this->request->data['User']['password'], true, '1 year');
                } else {
                    $this->Cookie->destroy();
                }
                /*
                 * Check if email or username is passed in form
                 */
                $uname = $this->request->data['User']['username'];
                //login via email
                if (filter_var($uname, FILTER_VALIDATE_EMAIL)) {
                    $u = $this->User->findByemail($uname);
                } else { //login via username
                    $u = $this->User->findByusername($uname);
                }
                if ($u) {
                    $this->request->data['User']['username'] = $u['User']['username'];
                    /*                 * *
                     * Error if user is not active
                     */
                    if ($u['User']['user_status'] != 'active') {
                        $this->Session->setFlash(__('Sorry! Your account is not active.'), 'default', array('class' => 'alert alert-danger'));
                    } elseif ($this->Auth->login()) { //if logged in
                        $user_caps = $this->fetchCapabilitiesByRole($u['User']['role']);
                        $this->Session->write("Auth.User.privileges", array('capabilities' => $user_caps['capabilities'], 'geo_areas' => array()));
                        if ($u['User']['role'] == 'A' || $u['User']['role'] == 'RA' || $u['User']['role'] == 'Af' || $u['User']['role'] == 'MAfA' || $u['User']['role'] == 'FAA')
                            return $this->redirect(array('controller' => 'dashboard', 'action' => 'view'));
                        return $this->redirect($this->Auth->redirect());
                    }else { //if invalid
                        $this->Session->setFlash(__('Invalid username or password.'), 'default', array('class' => 'alert alert-danger'));
                    }
                } else {//if user does not exists
                    $this->Session->setFlash(__('User does not exists.'), 'default', array('class' => 'alert alert-danger'));
                }
            }
        }
    
    0 讨论(0)
  • 2020-11-30 06:46

    I think you need to know about CakePHP Security levels. Try to lower the security of your cakePHP. CakePHP's Config variables documentation. I had written a blog about it also a long ago.

    0 讨论(0)
  • 2020-11-30 06:51

    In your user controller:

    public function beforeFilter() {
        $this->Auth->allow(array('login', 'register'));
        parent::beforeFilter();
    }
    
    public function login() {
        if ($this->request->is('post')) {
    
            if ($this->Auth->login()) {
    
                // did they select the remember me checkbox?
                if ($this->request->data['User']['remember_me'] == 1) {
                    // 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('remember_me_cookie', $this->request->data['User'], true, '2 weeks');
                }
    
                return $this->redirect($this->Auth->redirect());
    
            } else {
                $this->Session->setFlash(__('Username or password is incorrect.'));
            }
        }
    
        $this->set(array(
            'title_for_layout' => 'Login'
        ));
    }
    
    public function logout() {
        // clear the cookie (if it exists) when logging out
        $this->Cookie->delete('remember_me_cookie');
    
        return $this->redirect($this->Auth->logout());
    }
    

    In the login view:

    <h1>Login</h1>
    
    <?php echo $this->Form->create('User'); ?>
        <?php echo $this->Form->input('username'); ?>
        <?php echo $this->Form->input('password'); ?>
        <?php echo $this->Form->checkbox('remember_me'); ?> Remember Me
    <?php echo $this->Form->end('Login'); ?>
    

    In your AppController:

    public $components = array(
        'Session',
        'Auth',
        'Cookie'
    );
    
    public $uses = array('User');
    
    public function beforeFilter() {
        // set cookie options
        $this->Cookie->key = 'qSI232qs*&sXOw!adre@34SAv!@*(XSL#$%)asGb$@11~_+!@#HKis~#^';
        $this->Cookie->httpOnly = true;
    
        if (!$this->Auth->loggedIn() && $this->Cookie->read('remember_me_cookie')) {
            $cookie = $this->Cookie->read('remember_me_cookie');
    
            $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
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题