让分类、会员和示例工具共用同一账号,本地即可跑通登录与授权。 Co-authored-by: Cursor <cursoragent@cursor.com>
50 lines
1.8 KiB
PHP
50 lines
1.8 KiB
PHP
<?php
|
|
|
|
use App\Http\Middleware\EnsureActive;
|
|
use App\Http\Middleware\EnsureAdmin;
|
|
use Illuminate\Auth\AuthenticationException;
|
|
use Illuminate\Foundation\Application;
|
|
use Illuminate\Foundation\Configuration\Exceptions;
|
|
use Illuminate\Foundation\Configuration\Middleware;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
return Application::configure(basePath: dirname(__DIR__))
|
|
->withRouting(
|
|
web: __DIR__.'/../routes/web.php',
|
|
api: __DIR__.'/../routes/api.php',
|
|
commands: __DIR__.'/../routes/console.php',
|
|
health: '/up',
|
|
then: function () {
|
|
require base_path('routes/oauth.php');
|
|
},
|
|
)
|
|
->withMiddleware(function (Middleware $middleware) {
|
|
$middleware->statefulApi();
|
|
$middleware->alias([
|
|
'admin' => EnsureAdmin::class,
|
|
'active' => EnsureActive::class,
|
|
]);
|
|
$middleware->validateCsrfTokens(except: [
|
|
'oauth/token',
|
|
'oauth/introspect',
|
|
'oauth/revoke',
|
|
]);
|
|
})
|
|
->withExceptions(function (Exceptions $exceptions) {
|
|
$exceptions->render(function (ValidationException $e, Request $request) {
|
|
if ($request->expectsJson() || $request->is('api/*')) {
|
|
return response()->json([
|
|
'code' => 'validation_error',
|
|
'message' => collect($e->errors())->flatten()->first() ?: $e->getMessage(),
|
|
'errors' => $e->errors(),
|
|
], 422);
|
|
}
|
|
});
|
|
$exceptions->render(function (AuthenticationException $e, Request $request) {
|
|
if ($request->expectsJson() || $request->is('api/*')) {
|
|
return response()->json(['message' => '请先登录', 'code' => 'unauthenticated'], 401);
|
|
}
|
|
});
|
|
})->create();
|