Setting team_id in session when authenticating

Hi all, filament newbie here. I'm looking into using filament for a new project, using Spatie laravel-permission via the "Spatie Roles Permissions" filament plugin. I'm still figuring it out and whether it's a good fit for what I need (I think it will be). But I'm hitting some (basic) walls. As per the following: https://spatie.be/docs/laravel-permission/v6/basic-usage/teams-permissions#content-working-with-teams-permissions "After implementing a solution for selecting a team on the authentication process (for example, setting the team_id of the currently selected team on the session: session(['team_id' => $team->team_id]); )" Firstly: where do I set the team_id in session exactly? I'm unsure as to how I hook into the authentication process. Thanks in advance!
8 Replies
toeknee
toeknee2d ago
I placed a current_team_id on the user table, then on a custom middleware we can do
setPermissionsTeamId(auth()->user()?->current_team_id);
setPermissionsTeamId(auth()->user()?->current_team_id);
On a tenant check I would do:
$user = auth()->user();
$filamentTenant = \Filament\Facades\Filament::getTenant();

if ($filamentTenant?->id !== $user->current_team_id) {
$user->switchTeam($filamentTenant);
}
$user = auth()->user();
$filamentTenant = \Filament\Facades\Filament::getTenant();

if ($filamentTenant?->id !== $user->current_team_id) {
$user->switchTeam($filamentTenant);
}
ensuring the tenant id is always up to date
MattPurland
MattPurlandOP2d ago
I was reluctant to changing any models, but that makes total sense. Thanks, I'll give it a go
toeknee
toeknee2d ago
No problem, current means they can go to that tenant on login too.
MattPurland
MattPurlandOP2d ago
To clarify, switchTeam is available on the user model because it implements HasTenants? Apologies, I'm still figuring it all out
toeknee
toeknee2d ago
No sorry switchTeam is a custom function from jetstream/teams. But the basis is there
MattPurland
MattPurlandOP2d ago
Ah right, I'm not using JetStream
toeknee
toeknee2d ago
That's fine, it's just basically saying to update the current users team so: $user->update(['current_team_id' => Filament::getTenant()->id])
MattPurland
MattPurlandOP2d ago
Thought so, thanks

Did you find this page helpful?