PHP code example of black-bits / key-secret-api-authentication

1. Go to this page and download the library: Download black-bits/key-secret-api-authentication 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/ */

    

black-bits / key-secret-api-authentication example snippets


class Project extends KeySecretAuthenticatableModel
{
    // ...
}

'guards' => [
    // ... 
    
    'api' => [
        'driver' => 'key_secret',
        'provider' => 'key_secret',
    ],
],

'providers' => [
    'users' => [
        'driver' => 'eloquent',
        'model' => App\User::class,
    ],

    'key_secret' => [
        'driver' => 'eloquent',
        'model' => App\Project::class,
    ],
],

protected $middlewareGroups = [
    'web' => [
       // ...
    ],

    'api' => [
        'auth:api',
        'throttle:60,1',
        'bindings',
    ],
];

Route::get('test', function (Request $request) {
    return "hello world - " . $request->user()->name;
});

// Be aware, that "$request->user()->name" will return the property "name" from our Project-Model and not from the referenced User-Model.

$key    = 'abc'
$secret = '12345'
$token  = base64_encode($key . ':' . $secret);