From 47eb6a35f92e093704e025b68246b2a6900b245a Mon Sep 17 00:00:00 2001 From: Fred Bradley Date: Tue, 20 Jun 2023 16:15:14 +0100 Subject: [PATCH] api auth issues --- app/Http/Kernel.php | 2 ++ app/Http/Middleware/Authenticate.php | 6 +++--- app/Http/Middleware/ForceJsonResponse.php | 22 ++++++++++++++++++++++ app/Providers/RouteServiceProvider.php | 4 +--- routes/v1.php | 7 +++---- 5 files changed, 31 insertions(+), 10 deletions(-) create mode 100644 app/Http/Middleware/ForceJsonResponse.php diff --git a/app/Http/Kernel.php b/app/Http/Kernel.php index 9647034..ce5c053 100644 --- a/app/Http/Kernel.php +++ b/app/Http/Kernel.php @@ -2,6 +2,7 @@ namespace App\Http; +use App\Http\Middleware\ForceJsonResponse; use Illuminate\Foundation\Http\Kernel as HttpKernel; use Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful; @@ -41,6 +42,7 @@ class Kernel extends HttpKernel ], 'api' => [ + ForceJsonResponse::class, EnsureFrontendRequestsAreStateful::class, 'throttle:60,1', \Illuminate\Routing\Middleware\SubstituteBindings::class, diff --git a/app/Http/Middleware/Authenticate.php b/app/Http/Middleware/Authenticate.php index 704089a..6e8dc35 100644 --- a/app/Http/Middleware/Authenticate.php +++ b/app/Http/Middleware/Authenticate.php @@ -14,8 +14,8 @@ class Authenticate extends Middleware */ protected function redirectTo($request) { - if (! $request->expectsJson()) { - return route('login'); - } +// if (! $request->expectsJson()) { +// return route('login'); +// } } } diff --git a/app/Http/Middleware/ForceJsonResponse.php b/app/Http/Middleware/ForceJsonResponse.php new file mode 100644 index 0000000..acb58fe --- /dev/null +++ b/app/Http/Middleware/ForceJsonResponse.php @@ -0,0 +1,22 @@ +headers->set('Accept', 'application/json'); + + return $next($request); + } +} diff --git a/app/Providers/RouteServiceProvider.php b/app/Providers/RouteServiceProvider.php index 73ac25c..c6772c1 100644 --- a/app/Providers/RouteServiceProvider.php +++ b/app/Providers/RouteServiceProvider.php @@ -30,8 +30,6 @@ class RouteServiceProvider extends ServiceProvider */ public function boot() { - // - parent::boot(); } @@ -80,7 +78,7 @@ protected function mapWebRoutes() */ protected function mapApiRoutes() { - Route::middleware('api') + Route::middleware(['api', 'auth:sanctum']) ->prefix('v1') ->namespace($this->namespace) ->group(base_path('routes/v1.php')); diff --git a/routes/v1.php b/routes/v1.php index dc74d80..1f8cb28 100644 --- a/routes/v1.php +++ b/routes/v1.php @@ -14,9 +14,8 @@ | is assigned the "api" middleware group. Enjoy building your API! | */ -Route::middleware('auth:sanctum')->group(function() { - Route::get('asset/{id}', [AssetBankController::class, 'getAssetByID']); -}); -Route::middleware('auth:sanctum')->get('/me', function (Request $request) { + +Route::get('asset/{id}', [AssetBankController::class, 'getAssetByID']); +Route::get('/me', function (Request $request) { return $request->user(); });