1. Go to this page and download the library: Download kasitaw/api-key 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/ */
kasitaw / api-key example snippets
return [
/**
* Model use to configure Api Key
*/
'model' => [
'api_key' => Kasitaw\ApiKey\ApiKey::class, // Make sure use Kasitaw\ApiKey\Traits\HasApiKey.php trait if you use your own modal
],
/**
* Table name that reflected to the above model.
*/
'table_name' => [
'api_keys' => 'api_keys', // Table name to the above model
],
/**
* Column name being used to store generated api key
*/
'columns' => [
'key' => 'key',
],
/**
* Field name that being used to fetch the "apiKey". Either passed through query params or as a body.
*/
'request_key' => [
'api_key' => 'api_key',
],
/**
* Generated key length.
*/
'key_length' => 80,
];
namespace App;
use Kasitaw\ApiKey\Traits\HasApiKey;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use HasApiKey;
}
// Using `auth:api` as regular user authentication
Route::get('/users', function() {
//
})->middleware('auth:api');
// Using `auth:api_key` to authenticate user for external api
Route::get('/external/intergation/users', function() {
dd(request()->user());
// or using Auth::guard('api_key')->user()
// or using auth('api_key')->user()
})->middleware('auth:api_key');
$user->generateNewKey(); // By default will activate the key, pass `false` params to make it inactive
$user->activateAllKeys();
$user->activateKeyByKey('J1VFYTgUafp21ljEkanJYYnlY1j4REURXgAKzlwAUxABfCWPw4PBw9HKYbG4GWNvi125WUO0P2e7MmqC');
// or
$user->activateKeyByKey(
'J1VFYTgUafp21ljEkanJYYnlY1j4REURXgAKzlwAUxABfCWPw4PBw9HKYbG4GWNvi125WUO0P2e7MmqC',
'5c9fuEbAny4737an7hXC9VdNmDzd1yE0qn6Am9R8nNzJ0HWROn1daMJ19Lp36XLJlI5QIAkv6xYUkt6U'
);
$user->activateKeyByUuid('e0b9ed50-31b4-4ed6-a0f7-71490fa15ad6');
// or
$user->activateKeyByUuid(
'e0b9ed50-31b4-4ed6-a0f7-71490fa15ad6',
'597a67f8-9c19-4c2b-98ff-8020c0f7e360'
);
$user->revokeAllKeys();
$user->revokeKeyByKey('J1VFYTgUafp21ljEkanJYYnlY1j4REURXgAKzlwAUxABfCWPw4PBw9HKYbG4GWNvi125WUO0P2e7MmqC');
// or
$user->revokeKeyByKey(
'J1VFYTgUafp21ljEkanJYYnlY1j4REURXgAKzlwAUxABfCWPw4PBw9HKYbG4GWNvi125WUO0P2e7MmqC',
'5c9fuEbAny4737an7hXC9VdNmDzd1yE0qn6Am9R8nNzJ0HWROn1daMJ19Lp36XLJlI5QIAkv6xYUkt6U'
);
$user->revokeKeyByUuid('e0b9ed50-31b4-4ed6-a0f7-71490fa15ad6');
// or
$user->revokeKeyByUuid(
'e0b9ed50-31b4-4ed6-a0f7-71490fa15ad6',
'597a67f8-9c19-4c2b-98ff-8020c0f7e360'
);
$user->removeKeyByKey('J1VFYTgUafp21ljEkanJYYnlY1j4REURXgAKzlwAUxABfCWPw4PBw9HKYbG4GWNvi125WUO0P2e7MmqC');
// or
$user->removeKeyByKey(
'J1VFYTgUafp21ljEkanJYYnlY1j4REURXgAKzlwAUxABfCWPw4PBw9HKYbG4GWNvi125WUO0P2e7MmqC',
'5c9fuEbAny4737an7hXC9VdNmDzd1yE0qn6Am9R8nNzJ0HWROn1daMJ19Lp36XLJlI5QIAkv6xYUkt6U'
);
$user->removeKeyByUuid('e0b9ed50-31b4-4ed6-a0f7-71490fa15ad6');
// or
$user->removeKeyByUuid(
'e0b9ed50-31b4-4ed6-a0f7-71490fa15ad6',
'597a67f8-9c19-4c2b-98ff-8020c0f7e360'
);
$keys = $user->api_keys;
foreach($keys as $key) {
//
}
$user->removeAllKeys();
$keys = $user->api_keys()->active()->get();
foreach($keys as $key) {
//
}
$keys = $user->api_keys()->inActive()->get();
foreach($keys as $key) {
//
}
$uuid = 'e0b9ed50-31b4-4ed6-a0f7-71490fa15ad6';
$user->isKeyActive($uuid); // true/false, return null if key not found
// or
$key = 'J1VFYTgUafp21ljEkanJYYnlY1j4REURXgAKzlwAUxABfCWPw4PBw9HKYbG4GWNvi125WUO0P2e7MmqC';
$user->isKeyActive($key);