Whenever you visit the api endpoint, are you setting the Authorization header with the Bearer token correctly?
Oct 12, 2022
4
Level 3
Laravel showing {"message":"Unauthenticated."} when accessing api routes
I have an Vue + Laravel application. Here I am using laravel API routes to get the data. For the API auth I am using JWT from this tutorial: https://blog.logrocket.com/implementing-jwt-authentication-laravel-9/
Now I am getting:
{"message":"Unauthenticated."}
error message whne I access to this API route: http://localhost:3000/api/countries
Country.php (Model)
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
class Country extends Model
{
use HasFactory;
protected $table = 'countries';
protected $primaryKey = 'id_country';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'country_name' ,
'code' ,
];
}
CountryController.php (Controller)
<?php
namespace App\Http\Controllers;
use App\Models\Country;
use Illuminate\Http\Request;
class CountryController extends Controller
{
public function __construct()
{
$this->middleware('auth:api');
}
/**
* @return \Illuminate\Http\JsonResponse
*/
public function countries()
{
$country = Country::get();
return response()->json($country, 200);
}
}
api.php
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Auth\AuthController;
use App\Http\Controllers\CountryController;
Route::controller(CountryController::class)->group(function () {
Route::get('countries', 'countries')->name("countries");
});
Can you tell me what is wrong here and how can I solve it?
Please or to participate in this conversation.