I have this error in laravel since when I make the request I send the user's data and it generates the token but I try to add this and it tells me token not provided
$user = JWTAuth::toUser();
This is the code of my controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use JWTAuth;
use Tymon\JWTAuth\Contracts\JWTSubject;
use Tymon\JWTAuth\Exceptions\JWTException;
use App\Utils\Enums\EnumResponse;
class AuthenticateController extends Controller
{
public function __construct()
{
// Apply the jwt.auth middleware to all methods in this controller
// except for the authenticate method. We don't want to prevent
// the user from retrieving their token if they don't already have it
$this->middleware('jwt.auth', ['except' => ['authenticate']]);
}
public function authenticate(Request $request)
{
$credentials = $request->only('email', 'password');
$token = null;
try {
if (!$token = JWTAuth::attempt($credentials)) {
return response()->json([
'response' => 'error',
'message' => 'invalid_email_or_password',
]);
}
} catch (JWTAuthException $e) {
return response()->json([
'response' => 'error',
'message' => 'failed_to_create_token',
]);
}
return response()->json([
'response' => 'success',
'result' => [
'token' => $token,
],
]);
// if no errors are encountered we can return a JWT
}
This way it works but if I add this code it does not work, it gives me the error
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use JWTAuth;
use Tymon\JWTAuth\Contracts\JWTSubject;
use Tymon\JWTAuth\Exceptions\JWTException;
use App\Utils\Enums\EnumResponse;
class AuthenticateController extends Controller
{
public function __construct()
{
// Apply the jwt.auth middleware to all methods in this controller
// except for the authenticate method. We don't want to prevent
// the user from retrieving their token if they don't already have it
$this->middleware('jwt.auth', ['except' => ['authenticate']]);
}
public function authenticate(Request $request)
{
$credentials = $request->only('email', 'password');
$token = null;
try {
if (!$token = JWTAuth::attempt($credentials)) {
return response()->json([
'response' => 'error',
'message' => 'invalid_email_or_password',
]);
}
} catch (JWTAuthException $e) {
return response()->json([
'response' => 'error',
'message' => 'failed_to_create_token',
]);
}
$user = JWTAuth::toUser();
if( $user ) {
$data = [
'id' => $user->id,
'name' => $user->name,
'last_name' => $user->last_name,
'profile_photo' => $user->profile_photo,
'birth_date' => $user->birth_date,
'biography' => $user->biography,
'country_id' => $user->country_id,
'state_id' => $user->state_id,
'sex' => $user->sex,
'phone' => $user->phone,
'city_id' => $user->city_id,
'weight' => $user->weight,
'size' => $user->size,
'username' => $user->username,
'ranking' => $user->ranking,
'user_type' => $user->user_type,
'email' => $user->email,
'token' => $request->token
];
return responseRequest( EnumResponse::SUCCESS, $data );
}
Help please do not know how to use $user = JWTAuth::toUser();
to pass the token even if I pass it like this $user = JWTAuth::toUser($token);
It does not work