0

I need your help with my Laravel API HTTP Request. I am trying to get a response from my api. But it sends back "Error: "Request failed with status code 401" and the AuthenthicationException shows "Unauthenticated." . I have not found any solutions. I need your help... Thanks.

const response = (await rootApi.get(`/openSesame/targets/${id}`)).data;

My 'Api\TargetController

public function detail($id)
    {
        $spintaxInput = SpintaxInput::find($id);
        if (!$spintaxInput) {
            return response()->json(['error' => "Not found."], 404);
        }
        return response()->json($spintaxInput, 200);
    }

My api routes

Route::group(['middleware' => ['auth:web']], function () {

    Route::group(['prefix' => 'openSesame'], function () {
        Route::get('/targets/{id}', 'Api\TargetController@detail');
    });

});

dinorain
  • 1
  • 1

3 Answers3

0

you should use Sanctum to API authenticate

Shahadat Hossain
  • 947
  • 9
  • 24
0

Passport would be the way to go here.

check Passport

Now, to help you with this:

To be able to authenticate when you're going to make a request to the api, make sure to use the header: Authentication and the token followed by Bearer

A token example should be like this one:

Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJhdWQiOiIxIiwianRpIjoiZThmNjZiMTU0Yjg4OGM2YmI2ODg0ZDM2ZDU3NGYxY2FiODFjODgyMmI0MDc5NDVkMTNjM2I2MDdiMDc0MGNkNTI1MzgxZDU2NWJkNWUwZTciLCJpYXQiOjE1ODY3MDY1MzgsIm5iZiI6MTU4NjcwNjUzOCwiZXhwIjoxNjE4MjQyNTM4LCJzdWIiOiIxIiwic2NvcGVzIjpbXX0.Brf48KrE3smifCIo61_8alvU8Yi5atsMtLz5t7-drQ7xnpG0Lga9q7Wh8RDJaYLtxHltdMLRVfp2HtstVQO6XY8qE0GNqS2pfjwFGDJTChWDSbINzjRyX9rO9FdTbE95TPlh84e_PRQ7iWMDO_DQkB67YvuYieXJjWAzF5UBGjK9ZMSjjzHahHV-iCE4Si_i0DHP6bLDTksZd51jiYV7ptGr41lZCwnL09fNjtWtLYTl79OoxIzcEPZMUQ_l7QcoJPJUYa0-lvHAta9hbkzZHdskIOB9C6afel4VxPFEVI0RmP5glJBJFKu0A_0N80iJf7yhXqofk5muF-bFWv9_092os2h3-zt0bDjTb7jeSAY8CkgxRQ3oLtRQN9MBgxRdUechycdimbKpU6hhpGfJfsofHtJiRGAbh5Eddlq5AGjdZkRW6zu9CjFUFiivZHOO_JI3HmU39jXUQx8218Czb9_Z-iG02K0Bvtk0eilvZl3k6FjvDka3beb0Zg99Da5MKeYSMKXqn4U-mndQPPtsidWCwh4foskzv6mRvWhsGh8xN0zByCTIhML-ogqrIGNcDUsrgpi2E4eue4PZ9DfYIa740kYKnJLzpNC6_ilQPesb3MjqLLx9DBcUkuWH7fwKKA_OaxhIv2WiELUECjWuIIDMpNGuK_Kes0RbqedPvlg

When using API's, you will need to use that header in each request. Otherwise you will get a 401. The 401 means that you're not logged in.

DanielL
  • 99
  • 1
  • 10
0

You should use passport, like said.

And for auth middleware, replace it to:

middleware('auth:api')
Mondini
  • 580
  • 4
  • 16