Skip to content

Commit

Permalink
feat: implement Laravel sanctum (#670)
Browse files Browse the repository at this point in the history
  • Loading branch information
asbiin committed Apr 3, 2021
1 parent 984a1cf commit 00adad8
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 11 deletions.
1 change: 1 addition & 0 deletions app/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class Kernel extends HttpKernel
],

'api' => [
\Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
'throttle:api',
'sentry.context',
\Illuminate\Routing\Middleware\SubstituteBindings::class,
Expand Down
3 changes: 2 additions & 1 deletion app/Models/User/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Jobs\SendVerifyEmail;
use App\Models\Company\Company;
use App\Models\Company\Employee;
use Laravel\Sanctum\HasApiTokens;
use Illuminate\Notifications\Notifiable;
use Spatie\Activitylog\Traits\LogsActivity;
use Illuminate\Contracts\Auth\MustVerifyEmail;
Expand All @@ -14,7 +15,7 @@

class User extends Authenticatable implements MustVerifyEmail
{
use Notifiable, LogsActivity, HasFactory;
use Notifiable, LogsActivity, HasFactory, HasApiTokens;

/**
* The attributes that are mass assignable.
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"inertiajs/inertia-laravel": "^0",
"laravel/framework": "^8.0",
"laravel/helpers": "^1.1",
"laravel/sanctum": "^2.9",
"laravel/tinker": "^2.0",
"laravel/ui": "^3.0",
"mariuzzo/laravel-js-localization": "^1.7",
Expand Down
89 changes: 80 additions & 9 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 50 additions & 0 deletions config/sanctum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

return [

/*
|--------------------------------------------------------------------------
| Stateful Domains
|--------------------------------------------------------------------------
|
| Requests from the following domains / hosts will receive stateful API
| authentication cookies. Typically, these should include your local
| and production domains which access your API via a frontend SPA.
|
*/

'stateful' => explode(',', env(
'SANCTUM_STATEFUL_DOMAINS',
'localhost,localhost:3000,127.0.0.1,127.0.0.1:8000,::1'
)),

/*
|--------------------------------------------------------------------------
| 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,
],

];
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreatePersonalAccessTokensTable extends Migration
{
/**
* Run the migrations.
*/
public function up()
{
Schema::create('personal_access_tokens', function (Blueprint $table) {
$table->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.
*/
public function down()
{
Schema::dropIfExists('personal_access_tokens');
}
}
2 changes: 1 addition & 1 deletion routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
Route::get('invite/employee/{link}', 'Auth\\UserInvitationController@check');
Route::post('invite/employee/{link}/join', 'Auth\\UserInvitationController@join')->name('invitation.join');

Route::middleware(['auth', 'verified'])->group(function () {
Route::middleware(['auth:sanctum', 'verified'])->group(function () {
Route::get('home', 'HomeController@index')->name('home');
Route::post('search/employees', 'HeaderSearchController@employees');
Route::post('search/teams', 'HeaderSearchController@teams');
Expand Down

0 comments on commit 00adad8

Please sign in to comment.