Estoy creando un token de acceso para mi usuario en passport y en vez de obtener un string
de JWT
estoy obteniendo los datos del token en formato json
¿Por qué pasa esto?
Creación del token:
if(Auth::attempt($credentials)){
$user = Auth::user();
$token = $user->createToken('My Token', ['read-phrase'])->accessToken;
return response()->json(["token" => $token]);
}
La variable $token
queda como una instancia de Laravel\Sanctum\PersonalAccessToken
Lo que obtengo como respuesta del llamado a createToken()
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"
}
La documentación de passport indica que para obtener el token hay que llamar posteriormente a ->accessToken;
, lo que me retorna:
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 => "*"
]
}
He probado con una instalación totalmente limpia de laravel y passport y me ha entregado el JWT sin mayores problemas.
La salida del llamado a $user->createToken('My Token')->accessToken;
es un JWT como el siguiente:
eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJhdWQiOiIxIiwianRpIjoiN2ZmNjc1Nzc4YzQ5YTY0NjllMWM0ZjhjNWY1YmI0YTEwZTIxZWI3Y2Q2NGYyNzk4MWNhYWNjY2U3NDZjZWYxNmVhY2U4NGJhNWFjZGVjNzMiLCJpYXQiOjE2MDI3OTc1NzAsIm5iZiI6MTYwMjc5NzU3MCwiZXhwIjoxNjM0MzMzNTcwLCJzdWIiOiIxIiwic2NvcGVzIjpbXX0.n-XfuB0iqFxkLdWP925OuIC9Qdsm3nTCPuoIOEyStPf4udd6ym8xVQO6ivExVsLKZPpp__ZUl1xl8MqDLu5bCwrI7prpWKdd6fXtlmorqUNWQ6TJ65ZckBWdCdOs2Stxp8t_8Ojbkpid933ZkA3lyxQIz-h25_axG8ghciftWxfC5awOuKBq4r6PQggXGu9zJdkHdvoRr9YiqysmnSABRj4VqM_Rg5CDIoQ5bpVU8M3VYrYtQ6cgkb5vFP9L4_h7dEvW7LvBXOd8liM5dLVd8o4muok1DieGBDx85Kc_v3tPsUMw5j84rI5LPIKghvmxt637hmASxANoQ1LVSky7fpPOjVSyq2gJ0UO-HrBXtGIkZmEMiEe2Nh307cF6x5ZUPAYa7UhzZETrQAqid8j25gTdu2OxjOfudIZUJPs2WowXSImikEHvKYNv7up96wX3ZpQjsCFeODSlL-G2_wLjtRt_EBM7KwEugylDPydHaKa8iI6LTbFVmP400l3eONwNRcVCzUUMjV3rf5snp2boYTP07kdqcslXc88UPrSNgyEOCUmS8xeaAgbLOiM3fAyfzBSe4qJIoEIb1HH78eGYsH93A_Ae6GdF0GJBReCXyebVdBG2SvoOINZu7tIQYDfuX_HGFMiccnZrdltYOD7gm7xoC4oJ_38OPot9mcNRusE
La diferencia entre la instalación limpia y mi "aplicación" es la siguiente:
1.- Estoy utilizando UUID para los usuarios y passport
2.- Instalé Jetstream
3.- Instalé LightHouse (Graphql)
¿Cómo puedo darle el formato correspondiente a mi token para luego utilizarlos en los headers con Bearer?