From 8c00ae88300de42517ff7c5232caf5a6c45142f3 Mon Sep 17 00:00:00 2001 From: Faris Isa Date: Thu, 29 Apr 2021 09:52:48 +0700 Subject: [PATCH] add & install sanctum for API --- composer.json | 1 + composer.lock | 68 ++++++++++++++++++- config/sanctum.php | 50 ++++++++++++++ ...01_create_personal_access_tokens_table.php | 36 ++++++++++ 4 files changed, 153 insertions(+), 2 deletions(-) create mode 100644 config/sanctum.php create mode 100644 database/migrations/2019_12_14_000001_create_personal_access_tokens_table.php diff --git a/composer.json b/composer.json index 5e5af09..9dfc233 100644 --- a/composer.json +++ b/composer.json @@ -13,6 +13,7 @@ "fruitcake/laravel-cors": "^2.0", "guzzlehttp/guzzle": "^7.0.1", "laravel/framework": "^8.0", + "laravel/sanctum": "^2.10", "laravel/tinker": "^2.0", "phpoffice/phpword": "^0.18.1" }, diff --git a/composer.lock b/composer.lock index 36ae2b3..170d9e3 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "611bd4a54cc4a2e4c03fdfb66f7ccb21", + "content-hash": "e3859e1cdf2bd5a7374fb444d77fff1a", "packages": [ { "name": "asm89/stack-cors", @@ -1116,6 +1116,70 @@ ], "time": "2021-03-30T21:34:17+00:00" }, + { + "name": "laravel/sanctum", + "version": "v2.10.0", + "source": { + "type": "git", + "url": "https://github.com/laravel/sanctum.git", + "reference": "a08cfee365c6b6df3e91c8f43b92f7163ffc8a94" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/laravel/sanctum/zipball/a08cfee365c6b6df3e91c8f43b92f7163ffc8a94", + "reference": "a08cfee365c6b6df3e91c8f43b92f7163ffc8a94", + "shasum": "" + }, + "require": { + "ext-json": "*", + "illuminate/contracts": "^6.9|^7.0|^8.0", + "illuminate/database": "^6.9|^7.0|^8.0", + "illuminate/support": "^6.9|^7.0|^8.0", + "php": "^7.2|^8.0" + }, + "require-dev": { + "mockery/mockery": "^1.0", + "orchestra/testbench": "^4.0|^5.0|^6.0", + "phpunit/phpunit": "^8.0|^9.3" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.x-dev" + }, + "laravel": { + "providers": [ + "Laravel\\Sanctum\\SanctumServiceProvider" + ] + } + }, + "autoload": { + "psr-4": { + "Laravel\\Sanctum\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Taylor Otwell", + "email": "taylor@laravel.com" + } + ], + "description": "Laravel Sanctum provides a featherweight authentication system for SPAs and simple APIs.", + "keywords": [ + "auth", + "laravel", + "sanctum" + ], + "support": { + "issues": "https://github.com/laravel/sanctum/issues", + "source": "https://github.com/laravel/sanctum" + }, + "time": "2021-04-20T16:20:46+00:00" + }, { "name": "laravel/tinker", "version": "v2.6.1", @@ -7289,5 +7353,5 @@ "php": "^7.3" }, "platform-dev": [], - "plugin-api-version": "1.1.0" + "plugin-api-version": "2.0.0" } diff --git a/config/sanctum.php b/config/sanctum.php new file mode 100644 index 0000000..3ccc3ca --- /dev/null +++ b/config/sanctum.php @@ -0,0 +1,50 @@ + explode(',', env( + 'SANCTUM_STATEFUL_DOMAINS', + 'localhost,localhost:3000,127.0.0.1,127.0.0.1:8000,::1,'.parse_url(env('APP_URL'), PHP_URL_HOST) + )), + + /* + |-------------------------------------------------------------------------- + | Expiration Minutes + |-------------------------------------------------------------------------- + | + | This value controls the number of minutes until an issued token will be + | considered expired. If this value is null, personal access tokens do + | not expire. This won't tweak the lifetime of first-party sessions. + | + */ + + 'expiration' => null, + + /* + |-------------------------------------------------------------------------- + | Sanctum Middleware + |-------------------------------------------------------------------------- + | + | When authenticating your first-party SPA with Sanctum you may need to + | customize some of the middleware Sanctum uses while processing the + | request. You may change the middleware listed below as required. + | + */ + + 'middleware' => [ + 'verify_csrf_token' => App\Http\Middleware\VerifyCsrfToken::class, + 'encrypt_cookies' => App\Http\Middleware\EncryptCookies::class, + ], + +]; diff --git a/database/migrations/2019_12_14_000001_create_personal_access_tokens_table.php b/database/migrations/2019_12_14_000001_create_personal_access_tokens_table.php new file mode 100644 index 0000000..3ce0002 --- /dev/null +++ b/database/migrations/2019_12_14_000001_create_personal_access_tokens_table.php @@ -0,0 +1,36 @@ +bigIncrements('id'); + $table->morphs('tokenable'); + $table->string('name'); + $table->string('token', 64)->unique(); + $table->text('abilities')->nullable(); + $table->timestamp('last_used_at')->nullable(); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('personal_access_tokens'); + } +}