用户认证
基于 Laravel Sanctum 实现的用户统一认证服务,可参考官方文档
- 授权配置
config/auth.php
'defaults' => [ 'guard' => 'service' ] 'guards' => [ 'service' => [ 'driver' => 'sanctum', 'provider' => 'users' ] ]
- 用户模型
app/Models/User.php
<?php namespace App\Models; use Laravel\Sanctum\HasApiTokens; class User extends Model{ use HasApiTokens; }
- 数据库
personal_access_tokens
- 用户认证
use App\Models\User; use Illuminate\Support\Facades\Auth; //状态认证 $state=Auth::check(); //凭证下发 $user=User::find(1); $user->createToken('web')->plainTextToken;
- 路由守卫
Route::prefix('user')->middleware('auth')->group(function () { Route::get('/record','App\Http\Controllers\User@record'); });