Add current_team_id to users so the last active team is restored on login instead of always defaulting to the personal team. When a user belongs to multiple teams and has no valid stored choice, redirect them to a new team.select screen (SelectTeam Livewire component) to pick one, rather than silently choosing the first team. Update Fortify and OAuth login flows to use the new resolveStoredTeam() logic.
56 lines
1.7 KiB
PHP
56 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\User;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Symfony\Component\HttpKernel\Exception\HttpException;
|
|
|
|
class OauthController extends Controller
|
|
{
|
|
public function redirect(string $provider)
|
|
{
|
|
$socialite_provider = get_socialite_provider($provider);
|
|
|
|
return $socialite_provider->redirect();
|
|
}
|
|
|
|
public function callback(string $provider)
|
|
{
|
|
try {
|
|
$oauthUser = get_socialite_provider($provider)->user();
|
|
$email = trim((string) $oauthUser->email);
|
|
if ($email === '') {
|
|
abort(403, 'OAuth provider did not return an email address');
|
|
}
|
|
$email = strtolower($email);
|
|
$user = User::whereEmail($email)->first();
|
|
if (! $user) {
|
|
$settings = instanceSettings();
|
|
if (! $settings->is_registration_enabled) {
|
|
abort(403, 'Registration is disabled');
|
|
}
|
|
|
|
$user = User::create([
|
|
'name' => $oauthUser->name,
|
|
'email' => $email,
|
|
]);
|
|
}
|
|
Auth::login($user);
|
|
|
|
$team = $user->resolveStoredTeam();
|
|
if (! $team && $user->teams()->count() === 0) {
|
|
$team = $user->recreate_personal_team();
|
|
}
|
|
if ($team) {
|
|
session(['currentTeam' => $user->currentTeam = $team]);
|
|
}
|
|
|
|
return redirect('/');
|
|
} catch (\Exception $e) {
|
|
$errorCode = $e instanceof HttpException ? 'auth.failed' : 'auth.failed.callback';
|
|
|
|
return redirect()->route('login')->withErrors([__($errorCode)]);
|
|
}
|
|
}
|
|
}
|