Skip to content

Commit 75565d3

Browse files
committed
Add Sanctum
1 parent b7cc5d6 commit 75565d3

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed

config/sanctum.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
return [
4+
5+
/*
6+
|--------------------------------------------------------------------------
7+
| Stateful Domains
8+
|--------------------------------------------------------------------------
9+
|
10+
| Requests from the following domains / hosts will receive stateful API
11+
| authentication cookies. Typically, these should include your local
12+
| and production domains which access your API via a frontend SPA.
13+
|
14+
*/
15+
16+
'stateful' => explode(',', env('SANCTUM_STATEFUL_DOMAINS', sprintf(
17+
'%s%s',
18+
'localhost,localhost:3000,127.0.0.1,127.0.0.1:8000,::1',
19+
env('APP_URL') ? ','.parse_url(env('APP_URL'), PHP_URL_HOST) : ''
20+
))),
21+
22+
/*
23+
|--------------------------------------------------------------------------
24+
| Expiration Minutes
25+
|--------------------------------------------------------------------------
26+
|
27+
| This value controls the number of minutes until an issued token will be
28+
| considered expired. If this value is null, personal access tokens do
29+
| not expire. This won't tweak the lifetime of first-party sessions.
30+
|
31+
*/
32+
33+
'expiration' => null,
34+
35+
/*
36+
|--------------------------------------------------------------------------
37+
| Sanctum Middleware
38+
|--------------------------------------------------------------------------
39+
|
40+
| When authenticating your first-party SPA with Sanctum you may need to
41+
| customize some of the middleware Sanctum uses while processing the
42+
| request. You may change the middleware listed below as required.
43+
|
44+
*/
45+
46+
'middleware' => [
47+
'verify_csrf_token' => App\Http\Middleware\VerifyCsrfToken::class,
48+
'encrypt_cookies' => App\Http\Middleware\EncryptCookies::class,
49+
],
50+
51+
];
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class CreatePersonalAccessTokensTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('personal_access_tokens', function (Blueprint $table) {
17+
$table->bigIncrements('id');
18+
$table->morphs('tokenable');
19+
$table->string('name');
20+
$table->string('token', 64)->unique();
21+
$table->text('abilities')->nullable();
22+
$table->timestamp('last_used_at')->nullable();
23+
$table->timestamps();
24+
});
25+
}
26+
27+
/**
28+
* Reverse the migrations.
29+
*
30+
* @return void
31+
*/
32+
public function down()
33+
{
34+
Schema::dropIfExists('personal_access_tokens');
35+
}
36+
}

routes/api.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
use App\Http\Controllers\SideProjectController;
4+
use App\Http\Controllers\UserController;
35
use Illuminate\Http\Request;
46
use Illuminate\Support\Facades\Route;
57

@@ -39,3 +41,15 @@
3941
Route::middleware('auth:api')->get('/me', function (Request $request) {
4042
return $request->user();
4143
});*/
44+
45+
Route::post('users/', [UserController::class, 'store']);
46+
Route::get('users/{id}', [UserController::class, 'show']);
47+
Route::get('users/', [UserController::class, 'index']);
48+
49+
Route::apiResource('sideprojects', SideProjectController::class);
50+
51+
Route::post('users/auth', function (Request $request) {
52+
$token = $request->user()->createToken();
53+
54+
return ['token' => $token->plainTextToken];
55+
});

0 commit comments

Comments
 (0)