get_class(): Argument #1 ($object) must be of type object, null given Filament::auth()

I have a common resource for multiple users (User model, Artist Model, Affiliate Model) etc. in the list page, in table query I have like this.. to only load logged in user specific records
public function table(Table $table): Table
{
return $table
->modifyQueryUsing(fn ($query) => $query->published()->forUserType(get_class(Filament::auth()->user()));
}
public function table(Table $table): Table
{
return $table
->modifyQueryUsing(fn ($query) => $query->published()->forUserType(get_class(Filament::auth()->user()));
}
here we basicaly call where user_type = current auth user class this works fine .. but in test , when I do
$this->get(HelpModuleResource::getUrl('index'))->assertSuccessful();
$this->get(HelpModuleResource::getUrl('index'))->assertSuccessful();
I also add beforeEach for my pest test
beforeEach(function () {
$this->actingAs(Artist::factory()->create(), 'artist');
$this->actingAs(User::factory()->create(), 'web');
// other users
});
beforeEach(function () {
$this->actingAs(Artist::factory()->create(), 'artist');
$this->actingAs(User::factory()->create(), 'web');
// other users
});
I try to do this too chain with actingAs()->startSession() but still same error I get this error : get_class(): Argument #1 ($object) must be of type object, null given why Filament::auth()->user() is null while user actingAs and how to fix this..
9 Replies
Dennis Koch
Dennis Koch7mo ago
Your post would be more readable if you used code formatting. #✅┊rules
Krishzzi
KrishzziOP7mo ago
Done
Dennis Koch
Dennis Koch7mo ago
You are using the default auth for Filament?
Krishzzi
KrishzziOP7mo ago
I have 5 different panels with 5 different user models and guards. for each panel I have applied the specific guard with the authGuard function
$panel
->id('affiliate')
->path('affiliate')
->domain('demoaffiliate.jetpax.in')
->authGuard('affiliate')
->login()
$panel
->id('affiliate')
->path('affiliate')
->domain('demoaffiliate.jetpax.in')
->authGuard('affiliate')
->login()
how do I test this panels seperately because filament::auth() function does not work in test.
Dennis Koch
Dennis Koch7mo ago
Then you should use the auth guard as your second argument: $this->actingAs(User::factory()->create(), 'affiliate');
Krishzzi
KrishzziOP7mo ago
I already pass the guard when actingAs like this..
beforeEach(function () {
$this->actingAs(Artist::factory()->create(), 'artist');
$this->actingAs(User::factory()->create(), 'web');
// other users
});
beforeEach(function () {
$this->actingAs(Artist::factory()->create(), 'artist');
$this->actingAs(User::factory()->create(), 'web');
// other users
});
etc. but still not working. I pass guard with individual user models while actingAs but Filament::auth::user() or any method chained to it fails for null the test passed when I do like this.
$this->actingAs($user, $guard);
Filament::auth()->login($user);
$this->actingAs($user, $guard);
Filament::auth()->login($user);
So, when I do login with filament Filament::auth()->login($user); then the it gets Filament::auth()::user(), is actingAs() not enough for authenticate user
Dennis Koch
Dennis Koch7mo ago
Hm, interesting. I am not sure what might be the issue here.
Krishzzi
KrishzziOP7mo ago
so, any solution..
awcodes
awcodes7mo ago
I could be wrong, but how can more than one user be logged in for the test? Wouldn’t each actingAs() override the previous one?
Want results from more Discord servers?
Add your server