public function beforeFilter(Event $event) { parent::beforeFilter($event); if (!$this->Auth->user() && $this->Cookie->check('CookieAuth')) { $this->loadModel('Users'); $user = $this->Auth->identify(); if ($user) { $this->Auth->setUser($user); $user = $this->Users->newEntity($user); EventDispatcher::dispatch('User.afterLogin', $this, ['user' => $user]); $this->Cookie->write('CookieAuth', ['email' => $user->email, 'token' => $user->token]); $this->Users->save($user); } } }
/** * Sign in action * @return \Cake\Network\Response|null|void */ public function signin() { $this->onlyForGuest(); $this->viewBuilder()->autoLayout(false); $userEntity = $this->Users->newEntity(); if ($this->request->is(['post', 'put'])) { $user = $this->Auth->identify(); if ($user) { $this->Auth->setUser($user); $this->Users->patchEntity($userEntity, $user); EventDispatcher::dispatch('User.afterLogin', $this, ['user' => $userEntity]); if ($this->request->data('remember_me')) { $this->Cookie->write('CookieAuth', ['email' => $userEntity->email, 'token' => $userEntity->token]); } $this->Users->save($userEntity); $this->Flash->success(__('Vous êtes désormais connecté !')); return $this->redirect(['prefix' => 'admin', 'controller' => 'Pages', 'action' => 'dashboard']); } else { $this->Flash->error(__('Le couple d\'identifiants est incorrect.')); } } $this->setTitle(__('Connexion')); $this->set(compact('userEntity')); }