Skip to content

Commit

Permalink
api auth issues
Browse files Browse the repository at this point in the history
  • Loading branch information
fredbradley committed Jun 20, 2023
1 parent 0202821 commit 47eb6a3
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 10 deletions.
2 changes: 2 additions & 0 deletions app/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -41,6 +42,7 @@ class Kernel extends HttpKernel
],

'api' => [
ForceJsonResponse::class,
EnsureFrontendRequestsAreStateful::class,
'throttle:60,1',
\Illuminate\Routing\Middleware\SubstituteBindings::class,
Expand Down
6 changes: 3 additions & 3 deletions app/Http/Middleware/Authenticate.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ class Authenticate extends Middleware
*/
protected function redirectTo($request)
{
if (! $request->expectsJson()) {
return route('login');
}
// if (! $request->expectsJson()) {
// return route('login');
// }
}
}
22 changes: 22 additions & 0 deletions app/Http/Middleware/ForceJsonResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

class ForceJsonResponse
{
/**
* Handle an incoming request.
*
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
*/
public function handle(Request $request, Closure $next): Response
{
$request->headers->set('Accept', 'application/json');

return $next($request);
}
}
4 changes: 1 addition & 3 deletions app/Providers/RouteServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ class RouteServiceProvider extends ServiceProvider
*/
public function boot()
{
//

parent::boot();
}

Expand Down Expand Up @@ -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'));
Expand Down
7 changes: 3 additions & 4 deletions routes/v1.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});

0 comments on commit 47eb6a3

Please sign in to comment.