I am creating an access token for my user in passport and instead of getting a string
from JWT
I am getting the token data in format json
. Why is this happening?
Token creation:
if(Auth::attempt($credentials)){
$user = Auth::user();
$token = $user->createToken('My Token', ['read-phrase'])->accessToken;
return response()->json(["token" => $token]);
}
The variable $token
remains as an instance ofLaravel\Sanctum\PersonalAccessToken
What I get as a response from the call tocreateToken()
Laravel\Sanctum\NewAccessToken {#1415
+accessToken: Laravel\Sanctum\PersonalAccessToken {#478
#casts: array:2 [
"abilities" => "json"
"last_used_at" => "datetime"
]
#fillable: array:3 [
0 => "name"
1 => "token"
2 => "abilities"
]
#hidden: array:1 [
0 => "token"
]
#connection: "mysql"
#table: "personal_access_tokens"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: true
#attributes: array:8 [
"name" => "My Token"
"token" => "8063ed4786de4e6ad31a3d355dcc65463db2d6902d84f41d4cc85752fe90b87d"
"abilities" => "["read-phrase"]"
"tokenable_id" => "00d1783b-6904-492a-a249-b4e85a3ec70c"
"tokenable_type" => "App\Models\User"
"updated_at" => "2020-10-15 21:30:17"
"created_at" => "2020-10-15 21:30:17"
"id" => 8
]
#original: array:8 [
"name" => "My Token"
"token" => "8063ed4786de4e6ad31a3d355dcc65463db2d6902d84f41d4cc85752fe90b87d"
"abilities" => "["read-phrase"]"
"tokenable_id" => "00d1783b-6904-492a-a249-b4e85a3ec70c"
"tokenable_type" => "App\Models\User"
"updated_at" => "2020-10-15 21:30:17"
"created_at" => "2020-10-15 21:30:17"
"id" => 8
]
#changes: []
#classCastCache: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#visible: []
#guarded: array:1 [
0 => "*"
]
}
+plainTextToken: "8|IFDDKth2llbJpG9SgCDXaAWj19KekEhFlCrvDLRW"
}
The documentation of passport indicates that to obtain the token it is necessary to call subsequently ->accessToken;
, which returns me:
Laravel\Sanctum\PersonalAccessToken {#478
#casts: array:2 [
"abilities" => "json"
"last_used_at" => "datetime"
]
#fillable: array:3 [
0 => "name"
1 => "token"
2 => "abilities"
]
#hidden: array:1 [
0 => "token"
]
#connection: "mysql"
#table: "personal_access_tokens"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: true
#attributes: array:8 [
"name" => "My Token"
"token" => "23b10620f2e7b0f59e17337ba2d7b1a2da5ccbfea7a1fc0c95d4ca76f7d8eeb8"
"abilities" => "["read-phrase"]"
"tokenable_id" => "00d1783b-6904-492a-a249-b4e85a3ec70c"
"tokenable_type" => "App\Models\User"
"updated_at" => "2020-10-15 21:31:52"
"created_at" => "2020-10-15 21:31:52"
"id" => 9
]
#original: array:8 [
"name" => "My Token"
"token" => "23b10620f2e7b0f59e17337ba2d7b1a2da5ccbfea7a1fc0c95d4ca76f7d8eeb8"
"abilities" => "["read-phrase"]"
"tokenable_id" => "00d1783b-6904-492a-a249-b4e85a3ec70c"
"tokenable_type" => "App\Models\User"
"updated_at" => "2020-10-15 21:31:52"
"created_at" => "2020-10-15 21:31:52"
"id" => 9
]
#changes: []
#classCastCache: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#visible: []
#guarded: array:1 [
0 => "*"
]
}
I have tried with a totally clean installation of laravel and passport and it has delivered the JWT without major problems.
The output of the call to $user->createToken('My Token')->accessToken;
is a JWT like the following:
eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJhdWQiOiIxIiwianRpIjoiN2ZmNjc1Nzc4YzQ5YTY0NjllMWM0ZjhjNWY1YmI0YTEwZTIxZWI3Y2Q2NGYyNzk4MWNhYWNjY2U3NDZjZWYxNmVhY2U4NGJhNWFjZGVjNzMiLCJpYXQiOjE2MDI3OTc1NzAsIm5iZiI6MTYwMjc5NzU3MCwiZXhwIjoxNjM0MzMzNTcwLCJzdWIiOiIxIiwic2NvcGVzIjpbXX0.n-XfuB0iqFxkLdWP925OuIC9Qdsm3nTCPuoIOEyStPf4udd6ym8xVQO6ivExVsLKZPpp__ZUl1xl8MqDLu5bCwrI7prpWKdd6fXtlmorqUNWQ6TJ65ZckBWdCdOs2Stxp8t_8Ojbkpid933ZkA3lyxQIz-h25_axG8ghciftWxfC5awOuKBq4r6PQggXGu9zJdkHdvoRr9YiqysmnSABRj4VqM_Rg5CDIoQ5bpVU8M3VYrYtQ6cgkb5vFP9L4_h7dEvW7LvBXOd8liM5dLVd8o4muok1DieGBDx85Kc_v3tPsUMw5j84rI5LPIKghvmxt637hmASxANoQ1LVSky7fpPOjVSyq2gJ0UO-HrBXtGIkZmEMiEe2Nh307cF6x5ZUPAYa7UhzZETrQAqid8j25gTdu2OxjOfudIZUJPs2WowXSImikEHvKYNv7up96wX3ZpQjsCFeODSlL-G2_wLjtRt_EBM7KwEugylDPydHaKa8iI6LTbFVmP400l3eONwNRcVCzUUMjV3rf5snp2boYTP07kdqcslXc88UPrSNgyEOCUmS8xeaAgbLOiM3fAyfzBSe4qJIoEIb1HH78eGYsH93A_Ae6GdF0GJBReCXyebVdBG2SvoOINZu7tIQYDfuX_HGFMiccnZrdltYOD7gm7xoC4oJ_38OPot9mcNRusE
The difference between the clean install and my "app" is as follows:
1.- I am using UUID for users and passport
2.- I installed Jetstream
3.- I installed LightHouse (Graphql)
How can I give the corresponding format to my token to later use them in the headers with Bearer?
I noticed that the import of
trait
the in the classUser
was incorrect in relation to what the passport documentation mentioned. It is possible that this import was done by the jetstream installation ( but I'm not sure if that was the case, maybe it was a bad move by the code editor ).I currently had
After changing the import to what was mentioned in passport, everything worked as expected again.
The method
createToken()
in Passport returns an instance ofPersonalAccessTokenResult
which in turn implementsArrayable
andJsonable
, so you can do the following:Depending on your needs.
I show you an example that comes in the documentation:
Likewise, adapted to the flow of your code, it would look like this:
By the way, the object returned by the function
createToken
must be an instance ofLaravel\Sanctum\NewAccessToken
, I recommend that you check your configuration.