samedi 22 septembre 2018

How do I pass the api_token in Laravel to an api route?

OK, so I know this is probably a question that has been answered elsewhere but nothing I do works, so I must be doing something horribly wrong. I set up my Laravel project and then added laravel-api-auth to it in order to use an api_token. I've added the column to my users table, I've set up the env file with the token but my problem is, I have no idea how to pass the token to the API route, and whenever I find articles on it, no one ever gives examples, it's always "simply pass the api_token" but unfortunately it's never simple.

My project needs to retrieve a list of clients from a database, but only if the user logged in is authorised to view them. My routes file has this:

Route::middleware('auth.api')->get('/user', function (Request $request) {
    return $request->user();
});


Route::group(['middleware' => ['apiauth:MY_GREAT_APP']], function () {
    Route::prefix('clients')->group(function () {
        Route::get('/{accountId}', 'ClientController@getClients');
    });
});

But I always get a 401: Unauthorized error whenever I pass to it.

My apiauth.php file has this set up (using xxxxx as an example):

<?php

return [
    'services' => [

        'MY_GREAT_APP' => [
            'token' => 'xxxxx',
            'tokenName' => 'api_token',

            'allowJsonToken' => true,
            'allowBearerToken' => true,
            'allowRequestToken' => true,
        ]
    ],
];

Based on what I've read about passing the token as a get, I've tried:

Route::get('/{accountId}?api_token=xxxxx', 'ClientController@getClients');

But I just get a 404 error.

I also attempted passing it in my clients.js api file:

export function getClients(accountId) {
    return api.get(`clients/${accountId}?api_token=xxxxx`);
}

I don't know how to pass it as an Authorization header (I really have no idea what this is, even after researching it). The only information I gleaned is from this article but once again, there is no definitive example which I can use in my project.

So, anyone who can give me a straight example of how to pass this api_token would get my utmost appreciation, thank you.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire