CakePHP remember me with Auth

后端 未结 8 1928
爱一瞬间的悲伤
爱一瞬间的悲伤 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:52

    use CookeAuthenticate adapter:

    https://github.com/ceeram/Authenticate/blob/master/Controller/Component/Auth/CookieAuthenticate.php

    here more info:

    https://github.com/ceeram/Authenticate/wiki/Set-Cookie

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

    See this URL i think it is very help full to you.

    http://lecterror.com/articles/view/cakephp-and-the-infamous-remember-me-cookie

    Or Try this

    function login() {
        if ($this->Auth->user()) {
            if (!empty($this->data) && $this->data['User']['remember_me']) {
                $cookie = array();
                $cookie['username'] = $this->data['User']['username'];
                $cookie['password'] = $this->data['User']['password'];
                $this->Cookie->write('Auth.User', $cookie, true, COOKIE_EXPIRE);
                unset($this->data['User']['remember_me']);
            }
    
            $this->LogDetail->Write('activity','has logged IN');
            $this->redirect($this->Auth->redirect());
        }
    
        if (empty($this->data)) {
            $cookie = $this->Cookie->read('Auth.User');
            if (!is_null($cookie)) {
                if ($this->Auth->login($cookie)) {
                    $this->Session->destroy('Message.Auth'); # clear auth message, just in case we use it.
                    $this->LogDetail->Write('activity','has been authenticated via cookie and is now logged IN');
    
                    $this->redirect($this->Auth->redirect());
                } else {
                    $this->LogDetail->Write('activity','attempted to gain access with an invalid cookie');
                    $this->Cookie->destroy('Auth.User'); # delete invalid cookie
    
                    $this->Session->setFlash('Invalid cookie');
                    $this->redirect('login');
                }
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题