PHP code example of hafael / laravel-mesh-auth

1. Go to this page and download the library: Download hafael/laravel-mesh-auth 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/ */

    

hafael / laravel-mesh-auth example snippets


  ...
  class User extends Authenticatable //implements MustVerifyEmail
  {
      use ...
          HasApiTokens,//*
       * @var array
       */
      protected $fillable = [
          'name', 
          'lastname', 
          'email',
      ....

  ...
  
  protected $routeMiddleware = [
      ...
      'auth.mesh' => \Hafael\Mesh\Auth\Middlewares\MeshTokenMiddleware::class,
  ];


  

    $user = Auth::user();
    $sharedSecret = env('APP_SHARED_SECRET');

    //Dessa forma é possível identificar o usuário solicitante no lado servidor.
    $apiSecret = base64_encode( $user->id . '|' . $sharedSecret);
    
    //Solicite um token sanctum
    $response = Http::withHeaders([
      'X-API-KEY' => env('APP_SHARED_KEY'),
      'X-API-SECRET' => $apiSecret,
    ])->acceptJson()
      ->post('http://server-side-app.com/api/auth/token', [
          'name' => 'ClientAppName',
          'abilities' => ['*'],
      ]);

    $tokenName = $response['name'];
    $accessToken = $response['access_token'];
    $tokenAbilities = $response['abilities'];
    
    //Armazene o token relacionado ao usuário autenticado:
    $user->savePlainTextToken($accessToken, $tokenName, $tokenAbilities);

    //Crie uma nova requisição incluindo o token de acesso recém gerado.
    $authResponse = Http::withHeaders([
      'Authorization' => 'Bearer '. $accessToken,
    ])->acceptJson()
      ->get('http://server-side-app.com/api/user');

    var_dump($authResponse);

    die;