Show API Token after creation

I am trying to set up a CreateAction in a RelationManager for a model that uses the HasApiTokens-trait. The problem is that I didn't find any good way to show the plainTextToken to the user after creation. Several problems occur when using a modal/action here:
  1. The modal gets automatically closed after creation
  2. If I call $action->halt() the modal stays open but I have no way to update the field for the plaintexttoken
The closest I could get:
class TokensRelationManager extends RelationManager
{
    protected static string $relationship = 'tokens';

    #[Locked] 
    public ?string $plainTextToken = null;

    public function form(Form $form): Form
    {
        return $form
            ->schema([
                TextInput::make('name')
                    ->required()
                    ->maxLength(255),
                TextInput::make('abilities')
                    ->required(),
                TextInput::make('plainTextToken')
                    ->readOnly()
                    ->autocomplete(false)
                    ->label('Token')
                    ->columnSpanFull()
                    ->visible(fn () => $this->plainTextToken !== null)
                    ->formatStateUsing(fn () => $this->plainTextToken)
                    ->live(),
            ]);
    }

....
    public function table(Table $table): Table
    {
        return $table
            ...
            ->headerActions([
                CreateAction::make('createToken')
                    ->action(function (CreateAction $action, RelationManager $livewire, array $data) {
                        $device = $livewire->ownerRecord;
                        $this->plainTextToken = $device->createToken($data['name'], explode(',', $data['abilities']))->plainTextToken;
                        $action->halt();
                    })
            ]);
    }

Issues using this approach: Token is not updated right way, is not copyable and still shown on new modal
Was this page helpful?