How to add custom data in Laravel Passport token?

Viewed 383

I am using Laravel Passport to Auth user in my Laravel API. I would like to know if it is possible to add custom data to the token generated by Passport:

// /login endpoint
Auth::attempt($credentials);
$user = $request->user();
$token = $user->createToken('ACCESS_TOKEN')->accessToken;
return response()->json(['token' => $token]);

// /me endpoint
$user = $request()->user();
return response()->json(['user_data' => $user]);

This is how I'm actually using it, however, I'm just able to retrieve User Model information. I would like to add custom information to the JWT token and be able to retrieve that information in all endpoints, like /me, something like that:

// /login
... login logic
$token->customKey = 'custom value';
$token->foo = 'bar';

// /me
$user = $request->user();
$token = $user->token(); // returns the token
$customKey = $token->customKey; // this don't work
$foo = $token->foo; // this don't work

Is there any way to achieve this?

I'm using Laravel 8.0 and Laravel Passport 10.0.

0 Answers
Related