F
Filamentβ€’2y ago
John

Admin panel styling depending on authenticated user

I tried this:
namespace App\Providers;

use Auth;
use Filament\Facades\Filament;
use Illuminate\Support\ServiceProvider;

class FilamentServiceProvider extends ServiceProvider
{
public function boot()
{
// Register css
Filament::serving(function () {
Filament::registerViteTheme('resources/css/filament.css');
});

// Register custom css
if (Auth::user()->someVariable) {
Filament::registerStyles([
asset('css/some-styles.css'),
]);
}
}
}
namespace App\Providers;

use Auth;
use Filament\Facades\Filament;
use Illuminate\Support\ServiceProvider;

class FilamentServiceProvider extends ServiceProvider
{
public function boot()
{
// Register css
Filament::serving(function () {
Filament::registerViteTheme('resources/css/filament.css');
});

// Register custom css
if (Auth::user()->someVariable) {
Filament::registerStyles([
asset('css/some-styles.css'),
]);
}
}
}
1. It doesn't work because Auth::user() isn't loaded at this point in time. 2. It loads from /public. I'd prefer it if it would be included in the npm built styles. 3. The custom styles all need to be !important to overrule Filament styling. Any advise is appreciated!
2 Replies
Dan Harrin
Dan Harrinβ€’2y ago
do the auth check and asset registration inside serving() if you want the css built, you need another vite definition to build that file
John
JohnOPβ€’2y ago
Thanks @Dan Harrin ! All 3 solved πŸ™‚ For others that are looking for something similar, add your .css to vite.config.js and use this:
<?php

namespace App\Providers;

use Auth;
use Filament\Facades\Filament;
use Illuminate\Support\ServiceProvider;

class FilamentServiceProvider extends ServiceProvider
{
public function boot()
{
Filament::serving(function () {
Filament::registerViteTheme(array_filter([
'resources/css/filament.css',
Auth::user()->someVariable
? 'resources/css/some-styles.css'
: null
]));
});
}
}
<?php

namespace App\Providers;

use Auth;
use Filament\Facades\Filament;
use Illuminate\Support\ServiceProvider;

class FilamentServiceProvider extends ServiceProvider
{
public function boot()
{
Filament::serving(function () {
Filament::registerViteTheme(array_filter([
'resources/css/filament.css',
Auth::user()->someVariable
? 'resources/css/some-styles.css'
: null
]));
});
}
}
Want results from more Discord servers?
Add your server