0

I'm stuck and probably missing something really obvious.. but..

I'm trying to pass an array of roles via jwt to my SPA (not a jwt question - that bit works fine)

I get my list of role names via

$roles = $this->roles->pluck('title')->toArray();

the roles function is

public function roles()
    {
        return $this->belongsToMany(Role::class);
    }

in laravel this works fine and logging $roles out I see ["Admin"]

on the SPA response, however, I see the entire roles object joined to the user object (the user object is supposed to be passed over here) eg:

{
    "user": {
        "id": 1,
        "name": "Admin",
        "email": "[email protected]",
        "email_verified_at": null,
        "user_loggedin_state": null,
        "user_login_time": null,
        "user_login_hash": "",
        "user_log_out_time": null,
        "user_phone": "123456",
        "user_job": null,
        "created_at": null,
        "updated_at": null,
        "deleted_at": null,
        "team_id": 1,
        "roles": [
            {
                "id": 1,
                "title": "Admin",
                "created_at": null,
                "updated_at": null,
                "deleted_at": null,
                "pivot": {
                    "user_id": 1,
                    "role_id": 1
                }
            }
        ]
    }
}

What I want is

{
    "user": {
        "id": 1,
        "name": "Admin",
        "email": "[email protected]",
        "email_verified_at": null,
        "user_loggedin_state": null,
        "user_login_time": null,
        "user_login_hash": "",
        "user_log_out_time": null,
        "user_phone": "123456",
        "user_job": null,
        "created_at": null,
        "updated_at": null,
        "deleted_at": null,
        "team_id": 1,
        "roles": ["Admin"]
    }
}

I definitely pass the array to jwt, not the object - to validate this I wrapped the $roles in a function

public function rolesArray()
    {
        $roles = $this->roles->pluck('title')->toArray();
        Log::info($roles);
        return $roles;
    }

and the jwt fn

public function getJWTCustomClaims() {
        return [
            'roles'             => $this->rolesArray(),
        ];
    }  
4
  • Why you are not using API Resources ? Commented Feb 6, 2021 at 7:19
  • If you are not used resources before tell me to explain more about that Commented Feb 6, 2021 at 7:32
  • well using API Resources isn't going to solve this problem I think.. because the issue is that even when I can see I am returning an array, somehow Laravel managed to return the full object Commented Feb 6, 2021 at 8:01
  • Ok, I`m writing an answer for you. hopefully, this helps you. Commented Feb 6, 2021 at 8:06

2 Answers 2

0

Use API Resources for getting your own outputs.

Run this command to make a new resource for getting users:

php artisan make:resource UserListResource

In App/Http/Resourcers open UserListResource.php file

Change toArray method to:

public function toArray($request)
{
    return [
        'id' => $this->id,
        'name' => $this->name,
        'email' => $this->email,
        'email_verified_at' => $this->email_verified_at,
        'user_loggedin_state' => $this->user_loggedin_state,
        'user_login_time' => $this->user_login_time,
        'user_login_hash' => $this->user_login_hash,
        'user_log_out_time' => $this->user_log_out_time,
        'user_phone' => $this->user_phone,
        'user_job' => $this->user_job,
        'create_dates' => [
            'created_at_human' => $this->created_at->diffForHumans(),
            'created_at' => $this->created_at
        ],
        'update_dates' => [
            'updated_at_human' => $this->updated_at->diffForHumans(),
            'updated_at' => $this->updated_at
        ],
        'deleted_dates' => [
            'deleted_at_human' => $this->deleted_at->diffForHumans(),
            'deleted_at' => $this->deleted_at
        ],
        'team_id' => $this->team_id
        'roles' => new RolesShowResource($this->roles),
    ];
}

Then make Roles resources:

php artisan make:resource RolesShowResource

Open the RolesShowResource.php and do these changes:

public function toArray($request)
{
    return [
        'title' => $this->title,
    ];
}

So when you want to return objects try using the code below in fetching users:

return UserListResource::collection($users);
Sign up to request clarification or add additional context in comments.

Comments

0

ok the bleeding obvious was that all this data is of course in the JWT payload..

I was confused since the moment I added stuff in the custom fields - Laravel sent the User object across in the response as well (which I want to find a way of stopping since its in the clear)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.