Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

running laravel horizon in production throw a 403 error when requesting domain/horizon

I have testet laravel Horizon on my local environment and everything is working as expected. When I am switching to production domain/horizon throws a 403 error. I have set the gate in HorizonServiceProvider as stated in the documentation - First step is just to get access without auth. My gate now looks like this:

{
    Gate::define('viewHorizon', function ($user = null) {
        return true;
    });
}

Can anyone suggest what I am missing?

link to 403 error link to 401 error - Dashboard without data

like image 710
Der Heidi Avatar asked Dec 10 '22 00:12

Der Heidi


2 Answers

Check this GitHub comment: https://github.com/laravel/horizon/issues/563#issuecomment-480882947

You may have to register Horizon's service provider.

In config/app.php:

'providers' => [
        /*
         * Application Service Providers...
         */
        App\Providers\AppServiceProvider::class,
    ...
        App\Providers\TelescopeServiceProvider::class,
        App\Providers\HorizonServiceProvider::class,
    ],
like image 149
LobsterBaz Avatar answered Jan 23 '23 06:01

LobsterBaz


The error it's because horizon it's first go to the boot method, so i recommended you in your HorizonServiceProvider.php edit the boot method to allow your request like this:

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        parent::boot();

        Horizon::auth(function ($request) {
            if ($request->ajax()){
                return true;
            }
            else if (isset($request->let_me_go) && $request->let_me_go == 'ok'){
                return true;
            }else{
                throw new UnauthorizedHttpException('Unauthorized');
            }
        });
    }

So when you will go to your production server need to pass the parameter like this:

my-production-site.com/horizon/dashboard?let_me_go=ok

like image 21
Itamar Garcia Avatar answered Jan 23 '23 05:01

Itamar Garcia