filamentphp / filament

A collection of beautiful full-stack components for Laravel. The perfect starting point for your next app. Using Livewire, Alpine.js and Tailwind CSS.
https://filamentphp.com
MIT License
19.22k stars 2.96k forks source link

Unable to Redirect to Routes in Custom Error Handling #4872

Closed bobwurtz closed 2 years ago

bobwurtz commented 2 years ago

Package

filament/filament

Package Version

v2.16.41

Laravel Version

v9.38.0

Livewire Version

No response

PHP Version

PHP 8.1.6

Problem description

A blank, white page was displayed when trying to redirect to a Filament page after an exception was thrown.

Expected behavior

It should have redirected to the dashboard.

Steps to reproduce

Create a new Laravel app, then install Filament. Create a UserResource in Filament.

Then, in app/Exceptions/Handler.php use the following to customize how exceptions are handled:

/**
     * Render the exception into an HTTP response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function render($request, Throwable $e)
    {
        if($e instanceof AuthenticationException) {
            return redirect()->route('filament.auth.login');
        } else {
            return redirect()->route('filament.pages.dashboard');
        }
    }

Now try and access an invalid page, something like http://filamentProject.test/admin/users/2

Redirecting to the login page works fine (for unauthenticated users). However, redirecting to the dashboard (or any Filament resource page) ends with a blank white screen. I have included the log output below.

Reproduction repository

https://github.com/bobwurtz/filamentExceptionError

Relevant log output

2022-11-08 14:51:12] local.ERROR: Undefined property: Livewire\Redirector::$headers {"userId":1,"exception":"[object] (ErrorException(code: 0): Undefined property: Livewire\\Redirector::$headers at /Users/robertwurtz/Sites/filamentError/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php:191)
[stacktrace]
#0 /Users/robertwurtz/Sites/filamentError/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php(259): Illuminate\\Foundation\\Bootstrap\\HandleExceptions->handleError(2, 'Undefined prope...', '/Users/robertwu...', 191)
#1 /Users/robertwurtz/Sites/filamentError/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php(191): Illuminate\\Foundation\\Bootstrap\\HandleExceptions->Illuminate\\Foundation\\Bootstrap\\{closure}(2, 'Undefined prope...', '/Users/robertwu...', 191)
#2 /Users/robertwurtz/Sites/filamentError/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php(80): Illuminate\\Foundation\\Http\\Middleware\\VerifyCsrfToken->addCookieToResponse(Object(Illuminate\\Http\\Request), Object(Livewire\\Redirector))
#3 /Users/robertwurtz/Sites/filamentError/vendor/laravel/framework/src/Illuminate/Support/helpers.php(306): Illuminate\\Foundation\\Http\\Middleware\\VerifyCsrfToken->Illuminate\\Foundation\\Http\\Middleware\\{closure}(Object(Livewire\\Redirector))
#4 /Users/robertwurtz/Sites/filamentError/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php(82): tap(Object(Livewire\\Redirector), Object(Closure))
danharrin commented 2 years ago

How do you suggest we fix this? Seems like a Livewire limitation rather than anything to do with us.

bobwurtz commented 2 years ago

@danharrin I'm not sure how to fix it (or what is causing it). Since Filament can redirect correctly to the login page but not to a resource page I assumed this was a Filament issue. How is it a Livewire issue?

danharrin commented 2 years ago

Because Livewire binds its own Redirector to the container when a component is rendered, and then an exception is thrown from Livewire. Inside the exception hander it is expecting a different redirector instance. I think.

Login redirects are done in middleware, before the Livewire Redirector is bound.

maytham553-elite commented 1 week ago

Any solve ?