Jetstream 4.x defaultApiTokenPermissions ability format conflicts with UI comma-separated input
0 reputation · 23 Jan 2024, 11:14 UTC
0 reputation · 23 Jan 2024, 11:14 UTC
This question examines the serialization boundary between Jetstream's defaultApiTokenPermissions configuration array and the ApiTokenManager Livewire component when creating new personal access tokens. The configuration accepts ability names as strings, while the UI serializes input as comma-separated values for storage in the abilities JSON column on the personal_access_tokens table.
In Jetstream 4.x with Laravel 11+, the defaultApiTokenPermissions setting maps directly to the abilities field, but the frontend component expects comma-separated strings. This creates a mismatch when administrators customize default permissions, as the configuration value may not be properly serialized before UI rendering, and ability names containing spaces or special characters risk validation errors during token creation.
How does the framework reconcile configuration-level ability strings with UI-level comma-separated serialization? Does the Jetstream::defaultApiTokenPermissions array undergo automatic transformation, or must developers implement custom serialization? What validation occurs on the frontend for ability names that include spaces, and how does this affect token persistence?
31275 reputation · 23 Jan 2024, 15:19 UTC
Jetstream 4.x does not automatically transform the defaultApiTokenPermissions PHP array into a comma-separated string for the Livewire ApiTokenManager component. The configuration array is passed directly as the defaultPermissions property, but the component's permissions state (bound to the UI input) remains an empty string unless you override mount() to join the array yourself. On token creation, the component splits the permissions string by commas, trims each segment, and stores the resulting array in the abilities JSON column. Ability names containing commas will be split incorrectly; spaces within a segment are preserved.
config/jetstream.php defaultApiTokenPermissions (array) → ApiTokenManager::$defaultPermissions (array). No join occurs in the stock component.ApiTokenManager::$permissions (string) drives the comma-separated input. Default scaffolding leaves it empty on mount.store() does array_filter(array_map('trim', explode(',', $this->permissions))) before passing to createToken(). The abilities JSON column receives this array verbatim.php artisan vendor:publish --tag=jetstream-views (includes Livewire components in Jetstream 4.x).mount() in your App\Http\Livewire\ApiTokenManager (or the published path) to pre-fill the UI:
public function mount()
{
parent::mount();
$this->permissions = implode(', ', $this->defaultPermissions);
}store() with a custom parser (e.g., CSV-aware splitting).defaultApiTokenPermissions => ['read access', 'write access'] in config/jetstream.php.read access, write access after the mount() override.personal_access_tokens.abilities column: it should store ["read access","write access"].Are you using the Livewire stack or the Inertia (Vue/React) stack? The Inertia components handle permissions differently (array props, no comma-separated string), so the fix above applies only to Livewire.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.