PHP code example of supaapps / supaapps-guard

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

    

supaapps / supaapps-guard example snippets


'guards' => [
    'jwt' => [
        'driver' => 'supaapps-guard',
        'provider' => 'users',
    ],
],

'defaults' => [
    'guard' => 'jwt',
    ...

Route::middleware('auth:jwt')->get('/user', function (Request $request) {
    return [
        $request->user(),
        auth()->firstName(),
        auth()->lastName(),
        auth()->email(),
        auth()->scopes(),
        auth()->scopesArray(),
    ];
});

use Tests\TestCase;
use Supaapps\Guard\Tests\Concerns\GenerateJwtToken;

class CustomTest extends TestCase
{
    use GenerateJwtToken;

    public function testThatIAmActingAsUser(): void
    {
        $user = User::factory()->create();

        $this->withAccessTokenFor($user);

        $this->assertTrue(auth('jwt')->check());
        $this->assertTrue($user->id, auth('jwt')->id());
    }
}


use Supaapps\Guard\Tests\Concerns\GenerateJwtToken;

trait CreatesApplication
{
    use GenerateJwtToken;

    public function createApplication(): Application
    {
        ...

        $this->setAuthServerUrl();
        return $app;
    }
}



namespace Tests\Feature;

use Tests\TestCase;

class CustomTest extends TestCase
{
    public function itReturnsTheAuthUser(): void
    {
        $user = User::factory()->create();

        $this->withAccessTokenFor($user);

        // assume you have /user endpoint that
        // - uses auth:jwt middleware
        // - and returns auth user
        $response = $this->getJson('/user');

        $response->assertOk()
            ->assertJson($user->toArray());
    }
}