Skip to content

Commit

Permalink
fix: Access token middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
dogukanoksuz committed Jul 29, 2024
1 parent a21d51e commit 62c3f62
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
8 changes: 4 additions & 4 deletions app/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,13 @@ class Kernel extends HttpKernel
\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
Middleware\TrustProxies::class,
Middleware\EncryptCookies::class,
Middleware\CookieJWTAuthenticator::class,
Middleware\ClearTokenOnUnauthorized::class,
Middleware\APILogin::class,
];

protected $middlewareGroups = [
'web' => [
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\App\Http\Middleware\APILogin::class,
\Illuminate\Session\Middleware\AuthenticateSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
Expand All @@ -37,7 +35,9 @@ class Kernel extends HttpKernel
'api' => [
'throttle:600,1',
'bindings',
\App\Http\Middleware\APILocalization::class
Middleware\CookieJWTAuthenticator::class,
Middleware\ClearTokenOnUnauthorized::class,
Middleware\APILocalization::class,
],
];

Expand Down
11 changes: 10 additions & 1 deletion app/Http/Middleware/APILogin.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Http\Middleware;

use App\Models\AccessToken;
use App\User;
use Carbon\Carbon;
use Closure;
use Illuminate\Support\Facades\Auth;
Expand Down Expand Up @@ -40,7 +41,15 @@ public function handle($request, Closure $next)
'last_used_at' => Carbon::now()->toDateTimeString(),
'last_used_ip' => $request->ip(),
]);
Auth::loginUsingId($obj->user_id);

$token = auth('api')->login(User::find($obj->user_id));
if (! $token) {
return response()->json([
'message' => 'Invalid token.',
], 403);
}

$request->headers->set('Authorization', 'Bearer ' . $token);
}

return $next($request);
Expand Down

0 comments on commit 62c3f62

Please sign in to comment.