Skip to content

Commit

Permalink
prevent cookie conflicts when running the server and client on the sa…
Browse files Browse the repository at this point in the history
…me domain, by removing the session from delivery routes. (#52)
  • Loading branch information
holyfabi authored Jun 7, 2024
1 parent 7825afd commit 1f45e86
Show file tree
Hide file tree
Showing 9 changed files with 139 additions and 388 deletions.
68 changes: 0 additions & 68 deletions app/Http/Kernel.php

This file was deleted.

3 changes: 1 addition & 2 deletions app/Http/Middleware/RedirectIfAuthenticated.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace App\Http\Middleware;

use App\Providers\RouteServiceProvider;
use Closure;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
Expand All @@ -25,7 +24,7 @@ public function handle(Request $request, Closure $next, string ...$guards): Resp

foreach ($guards as $guard) {
if (Auth::guard($guard)->check()) {
return redirect(RouteServiceProvider::HOME);
return redirect()->intended('/home');
}
}

Expand Down
31 changes: 31 additions & 0 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@

namespace App\Providers;

use App\Models\Media;
use App\Models\User;
use App\Models\Version;
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\Facades\Route;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Support\ServiceProvider;

Expand All @@ -28,5 +35,29 @@ public function boot(): void
Relation::enforceMorphMap([
'user' => User::class,
]);

$this->configureRateLimiting();

Route::bind('media', function (string $identifier): Media {
$user = Auth::user() ?? User::whereName(Route::getCurrentRoute()->parameter('user'))->firstOrFail();
return $user->Media()->whereIdentifier($identifier)->firstOrFail();
});

Route::bind('version', function (int $versionNumber): Version {
$media = Route::getCurrentRoute()->parameter('media');
return $media->Versions()->whereNumber($versionNumber)->firstOrFail();
});
}

/**
* Configure the rate limiters for the application.
*
* @return void
*/
protected function configureRateLimiting(): void
{
RateLimiter::for('api', function (Request $request) {
return Limit::perMinute(60)->by($request->user()?->id ?: $request->ip());
});
}
}
66 changes: 0 additions & 66 deletions app/Providers/RouteServiceProvider.php

This file was deleted.

12 changes: 10 additions & 2 deletions bootstrap/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,20 @@
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;
use Illuminate\Routing\Middleware\SubstituteBindings;
use Illuminate\Support\Facades\Route;

return Application::configure(basePath: dirname(__DIR__))
->withRouting(
web: __DIR__.'/../routes/web.php',
commands: __DIR__.'/../routes/console.php',
// Using base_path() instead of __DIR__ because it's more uniform.
web: base_path('routes/web.php'),
api: base_path('routes/api.php'),
commands: base_path('routes/console.php'),
health: '/up',
then: function () {
Route::middleware(SubstituteBindings::class)
->group(base_path('routes/delivery.php'));
},
)
->withMiddleware(function (Middleware $middleware) {
//
Expand Down
2 changes: 0 additions & 2 deletions config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,6 @@
App\Providers\AuthServiceProvider::class,
// App\Providers\BroadcastServiceProvider::class,
App\Providers\EventServiceProvider::class,
App\Providers\RouteServiceProvider::class,

App\Providers\CdnHelperServiceProvider::class,
App\Providers\CloudStorageServiceProvider::class,
App\Providers\SqsFifoServiceProvider::class,
Expand Down
Loading

0 comments on commit 1f45e86

Please sign in to comment.