Refactor user switching logic and update UI

This commit is contained in:
Andras Bacsai 2024-02-26 11:48:35 +01:00
parent 600c43827a
commit 9268f9db1d
2 changed files with 13 additions and 17 deletions

View File

@ -15,30 +15,26 @@ public function mount()
if (!isCloud()) {
return redirect()->route('dashboard');
}
if (auth()->user()->id !== 0 && session('adminToken') === null) {
if (auth()->user()->id !== 0) {
return redirect()->route('dashboard');
}
$this->users = User::whereHas('teams', function ($query) {
$query->whereRelation('subscription', 'stripe_subscription_id', '!=', null);
})->get();
})->get()->filter(function ($user) {
return $user->id !== 0;
});
}
public function switchUser(int $user_id)
{
$user = User::find($user_id);
auth()->login($user);
if ($user_id === 0) {
Cache::forget('team:0');
session()->forget('adminToken');
} else {
$token_payload = [
'valid' => true,
];
$token = Crypt::encrypt($token_payload);
session(['adminToken' => $token]);
if (auth()->user()->id !== 0) {
return redirect()->route('dashboard');
}
session()->regenerate();
return refreshSession();
$user = User::find($user_id);
$team_to_switch_to = $user->teams->first();
Cache::forget("team:{$user->id}");
auth()->login($user);
refreshSession($team_to_switch_to);
return redirect(request()->header('Referer'));
}
public function render()
{

View File

@ -4,7 +4,7 @@
{{ auth()->user()->name }}
<h3 class="pt-4">Users</h3>
<div class="flex flex-wrap gap-2">
<div class="w-96 box" wire:click="switchUser('0')">
<div class="text-white cursor-pointer w-96 box-without-bg bg-coollabs-100" wire:click="switchUser('0')">
Root
</div>
@foreach ($users as $user)