lasthourcloud/app/Livewire/Security/ApiTokens.php

44 lines
985 B
PHP
Raw Normal View History

2023-10-20 12:51:01 +00:00
<?php
2023-12-07 18:06:32 +00:00
namespace App\Livewire\Security;
2023-10-20 12:51:01 +00:00
use Livewire\Component;
class ApiTokens extends Component
{
public ?string $description = null;
2024-06-10 20:43:34 +00:00
2023-10-20 12:51:01 +00:00
public $tokens = [];
2024-06-10 20:43:34 +00:00
2023-10-20 12:51:01 +00:00
public function render()
{
return view('livewire.security.api-tokens');
}
2024-06-10 20:43:34 +00:00
2023-10-20 12:51:01 +00:00
public function mount()
{
$this->tokens = auth()->user()->tokens;
}
2024-06-10 20:43:34 +00:00
2023-10-20 12:51:01 +00:00
public function addNewToken()
{
try {
$this->validate([
'description' => 'required|min:3|max:255',
]);
$token = auth()->user()->createToken($this->description);
$this->tokens = auth()->user()->tokens;
session()->flash('token', $token->plainTextToken);
} catch (\Exception $e) {
return handleError($e, $this);
}
}
2024-06-10 20:43:34 +00:00
2023-10-20 12:51:01 +00:00
public function revoke(int $id)
{
$token = auth()->user()->tokens()->where('id', $id)->first();
$token->delete();
$this->tokens = auth()->user()->tokens;
}
}