inertiajs / inertia-laravel

The Laravel adapter for Inertia.js.
https://inertiajs.com
MIT License
1.99k stars 222 forks source link

adding to HandleInertiaRequests->share array no longer works in version 1.2.0 #633

Closed Mrkars closed 1 month ago

Mrkars commented 1 month ago

I have this code block that use to work in version 1.0.0. It is no longer working in version 1.2.0. It use to return the auth.user along with permissions. Now it appears that permissions is just overwriting the entire array and that is all that is returned.

//HandleInertiaRequests.php

public function share(Request $request): array
{
      return array_merge(parent::share($request), [
            'auth.user.permissions' => fn () => $request->user()->getAllPermissions(),
      ]);
}
driesvints commented 1 month ago

@lepikhinb could this be related to the changes you made?

lepikhinb commented 1 month ago

@Mrkars can you share how does the original entire array look?

Mrkars commented 1 month ago

Sure thing. So in version 1.0.0, the array returned to the frontend looks like this:

{ "id": 1, "name": "User Name", "email": "email@email.com", "email_verified_at": "2024-05-15T19:29:47.000000Z", "two_factor_confirmed_at": null, "created_at": "2024-05-15T19:29:47.000000Z", "updated_at": "2024-05-15T19:35:48.000000Z", "deleted_at": null, "permissions": [ { "id": 1, "name": "Configuration-Read", "guard_name": "web", "created_at": "2024-05-15T19:29:47.000000Z", "updated_at": "2024-05-15T19:29:47.000000Z", "pivot": { "role_id": 1, "permission_id": 1 } }, { "id": 2, "name": "Configuration-Edit", "guard_name": "web", "created_at": "2024-05-15T19:29:47.000000Z", "updated_at": "2024-05-15T19:29:47.000000Z", "pivot": { "role_id": 1, "permission_id": 2 } }, ], "roles": [ { "id": 1, "name": "Super Admin", "guard_name": "web", "created_at": "2024-05-15T19:29:47.000000Z", "updated_at": "2024-05-15T19:29:47.000000Z", "pivot": { "model_type": "App\Models\User", "model_id": 1, "role_id": 1 } } ], "two_factor_enabled": false }

In version 2.0.0 it then looks like this:

{ "permissions": [ { "id": 1, "name": "Configuration-Read", "guard_name": "web", "created_at": "2024-05-15T19:29:47.000000Z", "updated_at": "2024-05-15T19:29:47.000000Z", "pivot": { "role_id": 1, "permission_id": 1 } }, { "id": 2, "name": "Configuration-Edit", "guard_name": "web", "created_at": "2024-05-15T19:29:47.000000Z", "updated_at": "2024-05-15T19:29:47.000000Z", "pivot": { "role_id": 1, "permission_id": 2 } }, ] }

As you can see the "permissions" array seems to replace the entire array instead of merging with it.

Mrkars commented 1 month ago

Some additional information, commenting out the one line of code in the "share" method in both versions 1.0.0 and 1.2.0. So basically leaving it as default. I then get back this:

{ "id": 1, "name": "User Name", "email": "email@email.com", "email_verified_at": "2024-05-15T19:29:47.000000Z", "two_factor_confirmed_at": null,
"created_at": "2024-05-15T19:29:47.000000Z", "updated_at": "2024-05-15T19:35:48.000000Z", "deleted_at": null, "permissions": [], "roles": [ { "id": 1, "name": "Super Admin", "guard_name": "web", "created_at": "2024-05-15T19:29:47.000000Z", "updated_at": "2024-05-15T19:29:47.000000Z", "pivot": { "model_type": "App\Models\User", "model_id": 1, "role_id": 1 } } ], "two_factor_enabled": false }

I believe it is the spatie/laravel-permission package that is adding the "permissions" and "roles" objects. Perhaps this is some kind of conflict between that package and me trying to add it manually.

reinink commented 1 month ago

Hey thanks for reporting, this should be fixed now (see #641), and it's been released as part of v1.3.0 👍