Files
qztool/backend/bootstrap/app.php
T
DevandCursor 60daff2365 搭建轻舟一期门户、后台和统一登录。
让分类、会员和示例工具共用同一账号,本地即可跑通登录与授权。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-09 16:46:35 +08:00

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();