Skip to content

An open-source, self-hostable PaaS alternative to Vercel, Heroku & Netlify that lets you easily deploy static sites, databases, full-stack applications and 360+ one-click services on your own servers.

fix(auth): redirect authenticated stale-token login submissions to the dashboard

peaklabs-dev37ddbdc37f92
diff --git a/app/Exceptions/Handler.php b/app/Exceptions/Handler.php
index 4b4df49711..c3a52ea356 100644
--- a/app/Exceptions/Handler.php
+++ b/app/Exceptions/Handler.php
@@ -4,9 +4,11 @@ namespace App\Exceptions;
use App\Models\InstanceSettings;
use App\Models\User;
+use App\Providers\RouteServiceProvider;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
+use Illuminate\Session\TokenMismatchException;
use Psr\Log\LogLevel;
use RuntimeException;
use Sentry\Laravel\Integration;
@@ -69,6 +71,11 @@ class Handler extends ExceptionHandler
*/
public function render($request, Throwable $e)
{
+ // A duplicate login or 2FA submission carries a stale token on an already authenticated session, see https://github.com/coollabsio/coolify/issues/10670
+ if ($e instanceof TokenMismatchException && $request->routeIs('login.store', 'two-factor.login.store') && $request->user()) {
+ return redirect()->intended(RouteServiceProvider::HOME);
+ }
+
// Handle authorization exceptions for API routes. Exceptions carrying
// an explicit status (e.g. denyAsNotFound) keep it via parent::render.
if ($e instanceof AuthorizationException && ! $e->hasStatus()) {
diff --git a/tests/Feature/Auth/TwoFactorChallengeAccessTest.php b/tests/Feature/Auth/TwoFactorChallengeAccessTest.php
index f0dd97cc31..a46f6b35c8 100644
--- a/tests/Feature/Auth/TwoFactorChallengeAccessTest.php
+++ b/tests/Feature/Auth/TwoFactorChallengeAccessTest.php
@@ -3,7 +3,11 @@
use App\Models\InstanceSettings;
use App\Models\Team;
use App\Models\User;
+use Illuminate\Contracts\Debug\ExceptionHandler;
use Illuminate\Foundation\Testing\RefreshDatabase;
+use Illuminate\Http\Request;
+use Illuminate\Session\TokenMismatchException;
+use Illuminate\Support\Facades\Route;
uses(RefreshDatabase::class);
@@ -78,3 +82,33 @@ it('renders 419 error page with login link instead of previous url', function ()
expect($view)->toContain('error-shell');
expect($view)->not->toContain('url()->previous()');
});
+
+it('redirects an authenticated stale two-factor submission home instead of showing 419', function () {
+ $request = Request::create('/two-factor-challenge', 'POST');
+ $request->setRouteResolver(fn () => Route::getRoutes()->match($request));
+ $request->setUserResolver(fn () => $this->user);
+
+ $response = app(ExceptionHandler::class)->render($request, new TokenMismatchException('CSRF token mismatch.'));
+
+ expect($response->getStatusCode())->toBe(302)
+ ->and($response->headers->get('Location'))->toBe(url('/'));
+});
+
+it('still returns 419 for a stale two-factor submission without an authenticated session', function () {
+ $request = Request::create('/two-factor-challenge', 'POST');
+ $request->setRouteResolver(fn () => Route::getRoutes()->match($request));
+
+ $response = app(ExceptionHandler::class)->render($request, new TokenMismatchException('CSRF token mismatch.'));
+
+ expect($response->getStatusCode())->toBe(419);
+});
+
+it('keeps the 419 for stale tokens on routes other than login and the two-factor challenge', function () {
+ $request = Request::create('/two-factor-challenge', 'GET');
+ $request->setRouteResolver(fn () => Route::getRoutes()->match($request));
+ $request->setUserResolver(fn () => $this->user);
+
+ $response = app(ExceptionHandler::class)->render($request, new TokenMismatchException('CSRF token mismatch.'));
+
+ expect($response->getStatusCode())->toBe(419);
+});