Horizon: Horizon Auth Is Not Working When Route Is Cached

Created on 9 Nov 2017  ·  2Comments  ·  Source: laravel/horizon

php artisan route:cache;

Horizon::auth(function ($request) {
    dd('test');
});

Most helpful comment

My guess is that you're putting Horizon::auth() in your routes.php, routes/web.php or similar?

You may just as well call the function from for example App\Providers\AuthServiceProvider::boot() or similar; then the routes should still get cached, but Horizon::auth would still be called when trying to access the dashboard.

/**
 * Register any authentication / authorization services.
 *
 * @return void
 */
public function boot()
{
    $this->registerPolicies();

    // Setup Horizon access
    \Horizon::auth(function ($request) {
        return $request->user()->is_admin;
    });
}

All 2 comments

My guess is that you're putting Horizon::auth() in your routes.php, routes/web.php or similar?

You may just as well call the function from for example App\Providers\AuthServiceProvider::boot() or similar; then the routes should still get cached, but Horizon::auth would still be called when trying to access the dashboard.

/**
 * Register any authentication / authorization services.
 *
 * @return void
 */
public function boot()
{
    $this->registerPolicies();

    // Setup Horizon access
    \Horizon::auth(function ($request) {
        return $request->user()->is_admin;
    });
}

The above didn't work for me, but did lead me to this which works:

/**
 * Register any authentication / authorization services.
 *
 * @return void
 */
public function boot()
{
    $this->registerPolicies();

    Horizon::auth(function($request) {
        return true;
    });
}

Then make sure you put it behind a middleware, mine is ['web', 'auth', 'verified'] but you can create one for Horizon.

I can now php artisan route:cache and works fine. 🤷‍♂️

Was this page helpful?
0 / 5 - 0 ratings