2023-11-14 09:50:27 -08:00
|
|
|
<?php
|
|
|
|
|
2024-05-29 12:07:48 -07:00
|
|
|
namespace App\Livewire;
|
2023-11-14 09:50:27 -08:00
|
|
|
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
2023-11-15 14:27:01 -08:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2023-11-14 09:50:27 -08:00
|
|
|
use Illuminate\View\View;
|
|
|
|
use Livewire\Component;
|
|
|
|
|
|
|
|
class PersonalAccessTokens extends Component
|
|
|
|
{
|
2023-11-15 14:51:09 -08:00
|
|
|
public $name;
|
2023-11-15 15:24:54 -08:00
|
|
|
public $newTokenString;
|
2023-11-15 18:19:03 -08:00
|
|
|
|
2023-11-16 09:24:54 -08:00
|
|
|
protected $listeners = ['openModal' => 'autoFocusModalEvent'];
|
|
|
|
|
2023-11-16 09:42:37 -08:00
|
|
|
//this is just an annoying thing to make the modal input autofocus
|
|
|
|
public function autoFocusModalEvent(): void
|
2023-11-16 09:24:54 -08:00
|
|
|
{
|
2024-05-29 12:17:36 -07:00
|
|
|
$this->dispatch('autoFocusModal');
|
2023-11-16 09:24:54 -08:00
|
|
|
}
|
|
|
|
|
2023-11-14 09:50:27 -08:00
|
|
|
public function render()
|
|
|
|
{
|
|
|
|
return view('livewire.personal-access-tokens', [
|
2024-07-04 12:49:22 -07:00
|
|
|
'tokens' => auth()->user()->tokens,
|
2023-11-14 09:50:27 -08:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function rules(): array
|
|
|
|
{
|
|
|
|
return [
|
2023-11-15 14:55:21 -08:00
|
|
|
'name' => 'required|string|max:255',
|
2023-11-14 09:50:27 -08:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function createToken(): void
|
|
|
|
{
|
2023-11-15 18:29:14 -08:00
|
|
|
$this->validate();
|
|
|
|
|
2024-07-04 12:49:22 -07:00
|
|
|
$newToken = auth()->user()->createToken($this->name);
|
2023-11-15 15:24:54 -08:00
|
|
|
|
|
|
|
$this->newTokenString = $newToken->accessToken;
|
|
|
|
|
2024-05-29 12:17:36 -07:00
|
|
|
$this->dispatch('tokenCreated', token: $newToken->accessToken);
|
2023-11-14 09:50:27 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function deleteToken($tokenId): void
|
|
|
|
{
|
2023-11-15 14:27:01 -08:00
|
|
|
//this needs safety (though the scope of auth::user might kind of do it...)
|
|
|
|
//seems like it does, test more
|
2024-07-04 12:49:22 -07:00
|
|
|
auth()->user()->tokens()->find($tokenId)?->delete();
|
2023-11-14 09:50:27 -08:00
|
|
|
}
|
|
|
|
}
|