fix(auth): keep the current team only in the session

Login, registration and OAuth set a currentTeam attribute on the user model
that is not a database column, so a save of the user in the same request
would fail. Keep the team in the session and read it with currentTeam().

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Andras Bacsai
2026-09-28 11:02:19 +02:00
co-authored by Claude Opus 5.5
parent f09de9c359
commit df9f1555b3
3 changed files with 6 additions and 6 deletions
+1 -1
View File
@@ -81,7 +81,7 @@ class CreateNewUser implements CreatesNewUsers
}
}
// Set session variable
session(['currentTeam' => $user->currentTeam = $team]);
session(['currentTeam' => $team]);
return $user;
}
+4 -4
View File
@@ -27,7 +27,7 @@ class FortifyServiceProvider extends ServiceProvider
public function toResponse($request)
{
// First user (root) will be redirected to /settings instead of / on registration.
if ($request->user()->currentTeam->id === 0) {
if ($request->user()->currentTeam()?->id === 0) {
return redirect()->route('settings.index');
}
@@ -88,9 +88,9 @@ class FortifyServiceProvider extends ServiceProvider
if (! $user->teams()->where('team_id', $invitation->team->id)->exists()) {
$user->teams()->attach($invitation->team->id, ['role' => $invitation->role]);
}
$user->currentTeam = $invitation->team;
$team = $invitation->team;
$invitation->delete();
session(['currentTeam' => $user->currentTeam]);
session(['currentTeam' => $team]);
} else {
// Restore the last active team; only fall back when unambiguous.
$team = $user->resolveStoredTeam();
@@ -98,7 +98,7 @@ class FortifyServiceProvider extends ServiceProvider
$team = $user->recreate_personal_team();
}
if ($team) {
session(['currentTeam' => $user->currentTeam = $team]);
session(['currentTeam' => $team]);
}
// Otherwise (multiple teams, no stored choice) leave the session
// team unset so the user is sent to the team-selection screen.
+1 -1
View File
@@ -29,7 +29,7 @@ class OauthLoginService
: $this->resolveOauthUser($oauthUser, $oauthSetting, $email);
$team = $user->currentTeam() ?? $user->teams()->first() ?? $user->recreate_personal_team();
session(['currentTeam' => $user->currentTeam = $team]);
session(['currentTeam' => $team]);
if ($this->requiresTwoFactorChallenge($user)) {
Auth::logout();