Last active
February 14, 2018 09:14
-
-
Save josephxanderson/1d488583043ee654f6e00b9c656fd45c to your computer and use it in GitHub Desktop.
[See How to Implement.txt below] This Gist shows how to implement a Client model observer for Laravel Passport so that you don't have to be stuck with that weird autoincrementing Client ID.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Observers; | |
use Laravel\Passport\Client; | |
class ClientObserver | |
{ | |
/** | |
* Listen to the Client creating event. | |
* | |
* @param \Laravel\Passport\Client $client | |
* @return void | |
*/ | |
public function creating(Client $client) | |
{ | |
// Set the model ID. | |
$client->setAttribute('id', Randomizer::createNumericId()); // This is where you will need to update this code to create the type of random, numeric ID you want. | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Providers; | |
use Illuminate\Support\ServiceProvider; | |
use Laravel\Passport\Client; | |
use Laravel\Passport\Passport; | |
use App\Observers\ClientObserver; | |
class AppServiceProvider extends ServiceProvider | |
{ | |
/** | |
* Bootstrap any application services. | |
* | |
* @return void | |
*/ | |
public function boot() | |
{ | |
// Register model observers. | |
Client::observe(ClientObserver::class); | |
} | |
/** | |
* Register any application services. | |
* | |
* @return void | |
*/ | |
public function register() | |
{ | |
Passport::ignoreMigrations(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Illuminate\Support\Facades\Schema; | |
use Illuminate\Database\Schema\Blueprint; | |
use Illuminate\Database\Migrations\Migration; | |
class CreateOauthClientsTable extends Migration | |
{ | |
/** | |
* Run the migrations. | |
* | |
* @return void | |
*/ | |
public function up() | |
{ | |
Schema::create('oauth_clients', function (Blueprint $table) { | |
$table->bigInteger('id')->unsigned(); | |
$table->integer('user_id')->index()->nullable(); | |
$table->string('name'); | |
$table->string('secret', 100); | |
$table->text('redirect'); | |
$table->boolean('personal_access_client'); | |
$table->boolean('password_client'); | |
$table->boolean('revoked'); | |
$table->timestamps(); | |
}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Illuminate\Support\Facades\Schema; | |
use Illuminate\Database\Schema\Blueprint; | |
use Illuminate\Database\Migrations\Migration; | |
class CreateOauthPersonalAccessClientsTable extends Migration | |
{ | |
/** | |
* Run the migrations. | |
* | |
* @return void | |
*/ | |
public function up() | |
{ | |
Schema::create('oauth_personal_access_clients', function (Blueprint $table) { | |
$table->bigInteger('id'); | |
$table->bigInteger('client_id')->index(); | |
$table->timestamps(); | |
}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Illuminate\Support\Facades\Schema; | |
use Illuminate\Database\Schema\Blueprint; | |
use Illuminate\Database\Migrations\Migration; | |
class CreateOauthAccessTokensTable extends Migration | |
{ | |
/** | |
* Run the migrations. | |
* | |
* @return void | |
*/ | |
public function up() | |
{ | |
Schema::create('oauth_access_tokens', function (Blueprint $table) { | |
$table->string('id', 100)->primary(); | |
$table->integer('user_id')->index()->nullable(); | |
$table->bigInteger('client_id'); | |
$table->string('name')->nullable(); | |
$table->text('scopes')->nullable(); | |
$table->boolean('revoked'); | |
$table->timestamps(); | |
$table->dateTime('expires_at')->nullable(); | |
}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Illuminate\Support\Facades\Schema; | |
use Illuminate\Database\Schema\Blueprint; | |
use Illuminate\Database\Migrations\Migration; | |
class CreateOauthAuthCodesTable extends Migration | |
{ | |
/** | |
* Run the migrations. | |
* | |
* @return void | |
*/ | |
public function up() | |
{ | |
Schema::create('oauth_auth_codes', function (Blueprint $table) { | |
$table->string('id', 100)->primary(); | |
$table->integer('user_id'); | |
$table->bigInteger('client_id'); | |
$table->text('scopes')->nullable(); | |
$table->boolean('revoked'); | |
$table->dateTime('expires_at')->nullable(); | |
}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Illuminate\Support\Facades\Schema; | |
use Illuminate\Database\Schema\Blueprint; | |
use Illuminate\Database\Migrations\Migration; | |
class CreateOauthRefreshTokensTable extends Migration | |
{ | |
/** | |
* Run the migrations. | |
* | |
* @return void | |
*/ | |
public function up() | |
{ | |
Schema::create('oauth_refresh_tokens', function (Blueprint $table) { | |
$table->string('id', 100)->primary(); | |
$table->string('access_token_id', 100)->index(); | |
$table->boolean('revoked'); | |
$table->dateTime('expires_at')->nullable(); | |
}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This Gist provides an example of how to implement a Client Model Observer for Laravel Passport. | |
This Observer will allow you to intercept the creation of a `Laravel\Passport\Client` model and set a custom id for it. | |
However, to implement this, you will need to handle the migrations yourself. In my case, the custom ID will be a random, large integer. So the database migrations set the `id` field to a type of `bigInteger`. | |
Take a look at this complete Gist carefully and implement it yourself. It's very easy. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment