PHP code example of mosaxiv / cakephp-token-verify
1. Go to this page and download the library: Download mosaxiv/cakephp-token-verify library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
mosaxiv / cakephp-token-verify example snippets
// app/src/Model/Entity/User.php
use Token\Model\Entity\TokenTrait;
class User extends Entity
{
use TokenTrait;
}
// app/src/Controller/UsersController.php
use Cake\Routing\Router;
use Token\Util\Token;
class UsersController extends AppController
{
public function forgotPassword()
{
if ($this->request->is('post')) {
$email = $this->request->getData('email');
$user = $this->Users->findByEmail($email)->first();
if ($user) {
$token = $user->tokenGenerate();
$url = Router::url(['controller' => 'User', 'action' => 'resetPassword', $token], true);
// send email
}
}
}
public function resetPassword($token)
{
$user = $this->Users->get(Token::getId($token));
if (!$user->tokenVerify($token)) {
throw new \Cake\Network\Exception\NotFoundException();
}
if ($this->request->is('post')) {
$user = $this->Users->patchEntity($user, $this->request->getData());
if ($this->Users->save($user)) {
// success
} else {
// error
}
}
}
}