I've been a couple of days and I can't find a way to generate a jwt token with Passport and I've tried everything, since this library doesn't work, it only works up to version 8: tymon/jwt-auth
I have been following a tutorial ( YouTube video ) but when creating the jwt I get a full object and not a token.
Code of the Login method in the AuthController
public function login(Request $request)
{
$loginData = $request->validate([
'email' => 'email|required',
'password' => 'required'
]);
if(!auth()->attempt($request->only('email','password'))){
return response()->json([
'status' => false,
'data' => [
'message'=>'No autorizado'
]
], 401);
}
$user = User::where('email', $request['email'])->FirstOrFail();
$token = $user->createToken('Token Name')->accessToken;
return response()->json([
'status' => true,
'data' => [
'message'=>$token
]
]);
}
and when I call it, it returns this response:
{
"status": true,
"data": {
"message": {
"name": "Token Name",
"abilities": [
"*"
],
"tokenable_id": 1,
"tokenable_type": "App\\Models\\User",
"updated_at": "2022-03-22T14:13:41.000000Z",
"created_at": "2022-03-22T14:13:41.000000Z",
"id": 6
}
}
}
My question is: is this line at the time of creating the token okay?
$token = $user->createToken('Token Name')->accessToken;
With what method can I generate a jwt bearer using passport?
It has happened to me, check that in the User model or where you are extending from Authenticable the use of passport is correct
use Laravel\Passport\HasApiTokens;
, sometimes it places it by default withSanctum