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();
})
]);
}
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
3 Replies
toeknee
toeknee3w ago
I would just use a Wizard, afterstatevalidated, you store the token etc, and on step 2, you render the token.
Nuekrato
NuekratoOP3w ago
Can I have a submit action on step 1 and an infolist on step 2 in a wizard?
toeknee
toeknee3w ago
Hmm yes you could load an info list as a custom live wire component in the form.

Did you find this page helpful?