PHP code example of vpremiss / livewire-nonceable

1. Go to this page and download the library: Download vpremiss/livewire-nonceable 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/ */

    

vpremiss / livewire-nonceable example snippets


  use Livewire\Component;
  use VPremiss\LivewireNonceable\Interfaces\Noncing;
  use VPremiss\LivewireNonceable\Traits\Nonceable;

  class FuzzySearch extends Component implements Noncing
  {
      use Nonceable;

      public function getNonces(): array
      {
          return [
              'complex-searching' => 5, // the nonce title, plus 5 seconds lasting in cache
              // 'heavy-processing' => 10, as another example
          ];
      }

      public function getNonceUniqueId(): string
      {
          return (string)auth()->user()->id; // After ensuring authentication, of course!
      }

      public function getNonceValidation(): bool
      {
          return auth()->user()->id === $this->getNonceUniqueId();
      }

      // This method is initiated securely
      protected function validateSearch($query)
      {
          // $validatedQuery = some validations

          $nonce = $this->generateNonce('complex-searching');

          $this->dispatch(
              'searching-began', // receive in the front-end (SPA)
              query: $validatedQuery,
              nonce: $nonce,
          );
      }

      // This is hit back from AlpineJS using axios
      public function complexSearch($responseFromApi, $nonce)
      {
          // Approach 1

          // Or use the opposite ! $this->doesNonceExist($title, $nonce) method
          if ($this->isNonceSense('complex-searching', $nonce)) {
              // throw new NoncenseException('Nonce mismatch. Somebody is playing around!');
          }

          $this->deleteNonce('complex-searching', $nonce);

          // Approach 2

          if (!$this->validatedNonce('complex-searching', $nonce)) {
              // throw or whatever but that's all, since it would have deleted the nonce otherwise
          }

          // do the complex searching now with an ease of mind...
      }
  }
  
bash
   php artisan vendor:publish --tag="livewire-nonceable-config"