-
-
Notifications
You must be signed in to change notification settings - Fork 109
Expand file tree
/
Copy pathCreatePersonalAccessToken.php
More file actions
61 lines (49 loc) · 1.79 KB
/
Copy pathCreatePersonalAccessToken.php
File metadata and controls
61 lines (49 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php
namespace App\Filament\Resources\PersonalAccessTokenResource\Pages;
use App\Filament\Resources\PersonalAccessTokenResource;
use App\Models\User;
use Filament\Notifications\Notification;
use Filament\Resources\Pages\CreateRecord;
use Illuminate\Database\Eloquent\Model;
class CreatePersonalAccessToken extends CreateRecord
{
protected static string $resource = PersonalAccessTokenResource::class;
protected function handleRecordCreation(array $data): Model
{
// We need to handle token creation specially since we can't store plain text tokens
/** @var User $user */
$user = User::find($data['tokenable_id']);
// Parse abilities - handle both string and array input
$abilities = $data['abilities'] ?? '*';
if (is_string($abilities)) {
$abilities = $abilities === '*' ? ['*'] : explode(',', $abilities);
$abilities = array_map('trim', $abilities);
}
$token = $user->createToken(
name: $data['name'],
abilities: $abilities,
expiresAt: $data['expires_at'] ?? null
);
// Store the plain text token to show to user
session(['new_api_token' => $token->plainTextToken]);
// Return the token model
return $token->accessToken;
}
protected function afterCreate(): void
{
$token = session('new_api_token');
if ($token) {
Notification::make()
->title('API Key Created Successfully')
->body("Your API key: {$token}")
->success()
->persistent()
->send();
session()->forget('new_api_token');
}
}
protected function getRedirectUrl(): string
{
return $this->getResource()::getUrl('index');
}
}