Skip to content

Commit 9801436

Browse files
committedJun 15, 2024
implement api and sanctum
1 parent f176c15 commit 9801436

File tree

7 files changed

+193
-2
lines changed

7 files changed

+193
-2
lines changed
 

‎app/Models/User.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
use Illuminate\Database\Eloquent\Factories\HasFactory;
77
use Illuminate\Foundation\Auth\User as Authenticatable;
88
use Illuminate\Notifications\Notifiable;
9+
use Laravel\Sanctum\HasApiTokens;
910

1011
class User extends Authenticatable
1112
{
12-
use HasFactory, Notifiable;
13+
use HasFactory, Notifiable,HasApiTokens;
1314

1415
/**
1516
* The attributes that are mass assignable.

‎bootstrap/app.php

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
return Application::configure(basePath: dirname(__DIR__))
88
->withRouting(
99
web: __DIR__.'/../routes/web.php',
10+
api: __DIR__.'/../routes/api.php',
1011
commands: __DIR__.'/../routes/console.php',
1112
health: '/up',
1213
)

‎composer.json

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"require": {
88
"php": "^8.2",
99
"laravel/framework": "^11.9",
10+
"laravel/sanctum": "^4.0",
1011
"laravel/tinker": "^2.9"
1112
},
1213
"require-dev": {

‎composer.lock

+65-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎config/sanctum.php

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
use Laravel\Sanctum\Sanctum;
4+
5+
return [
6+
7+
/*
8+
|--------------------------------------------------------------------------
9+
| Stateful Domains
10+
|--------------------------------------------------------------------------
11+
|
12+
| Requests from the following domains / hosts will receive stateful API
13+
| authentication cookies. Typically, these should include your local
14+
| and production domains which access your API via a frontend SPA.
15+
|
16+
*/
17+
18+
'stateful' => explode(',', env('SANCTUM_STATEFUL_DOMAINS', sprintf(
19+
'%s%s',
20+
'localhost,localhost:3000,127.0.0.1,127.0.0.1:8000,::1',
21+
Sanctum::currentApplicationUrlWithPort()
22+
))),
23+
24+
/*
25+
|--------------------------------------------------------------------------
26+
| Sanctum Guards
27+
|--------------------------------------------------------------------------
28+
|
29+
| This array contains the authentication guards that will be checked when
30+
| Sanctum is trying to authenticate a request. If none of these guards
31+
| are able to authenticate the request, Sanctum will use the bearer
32+
| token that's present on an incoming request for authentication.
33+
|
34+
*/
35+
36+
'guard' => ['web'],
37+
38+
/*
39+
|--------------------------------------------------------------------------
40+
| Expiration Minutes
41+
|--------------------------------------------------------------------------
42+
|
43+
| This value controls the number of minutes until an issued token will be
44+
| considered expired. This will override any values set in the token's
45+
| "expires_at" attribute, but first-party sessions are not affected.
46+
|
47+
*/
48+
49+
'expiration' => null,
50+
51+
/*
52+
|--------------------------------------------------------------------------
53+
| Token Prefix
54+
|--------------------------------------------------------------------------
55+
|
56+
| Sanctum can prefix new tokens in order to take advantage of numerous
57+
| security scanning initiatives maintained by open source platforms
58+
| that notify developers if they commit tokens into repositories.
59+
|
60+
| See: https://docs.github.com/en/code-security/secret-scanning/about-secret-scanning
61+
|
62+
*/
63+
64+
'token_prefix' => env('SANCTUM_TOKEN_PREFIX', ''),
65+
66+
/*
67+
|--------------------------------------------------------------------------
68+
| Sanctum Middleware
69+
|--------------------------------------------------------------------------
70+
|
71+
| When authenticating your first-party SPA with Sanctum you may need to
72+
| customize some of the middleware Sanctum uses while processing the
73+
| request. You may change the middleware listed below as required.
74+
|
75+
*/
76+
77+
'middleware' => [
78+
'authenticate_session' => Laravel\Sanctum\Http\Middleware\AuthenticateSession::class,
79+
'encrypt_cookies' => Illuminate\Cookie\Middleware\EncryptCookies::class,
80+
'validate_csrf_token' => Illuminate\Foundation\Http\Middleware\ValidateCsrfToken::class,
81+
],
82+
83+
];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*/
12+
public function up(): void
13+
{
14+
Schema::create('personal_access_tokens', function (Blueprint $table) {
15+
$table->id();
16+
$table->morphs('tokenable');
17+
$table->string('name');
18+
$table->string('token', 64)->unique();
19+
$table->text('abilities')->nullable();
20+
$table->timestamp('last_used_at')->nullable();
21+
$table->timestamp('expires_at')->nullable();
22+
$table->timestamps();
23+
});
24+
}
25+
26+
/**
27+
* Reverse the migrations.
28+
*/
29+
public function down(): void
30+
{
31+
Schema::dropIfExists('personal_access_tokens');
32+
}
33+
};

‎routes/api.php

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
use Illuminate\Http\Request;
4+
use Illuminate\Support\Facades\Route;
5+
6+
Route::get('/user', function (Request $request) {
7+
return $request->user();
8+
})->middleware('auth:sanctum');

0 commit comments

Comments
 (0)
Please sign in to comment.