简体   繁体   中英

How to authenticate for Laravel API?

I'm writing my first Laravel API as a project to get to know Laravel. However, it's more difficult than I expected - eventhough I wrote a full webapp in laravel first. Also, I'm new to Stackoverflow, so I hope my question is correct.

I've set up routes for my API, but now I also want to authenticate every API request. The routes I've set up can be found below:

Route::group(['prefix' => 'api/v1', 'before' => 'basic.once'], function ()
{

    Route::resource('lessons', 'LessonsController');

    Route::get('lessons/user/{userid}', 'LessonsController@user');

    Route::resource('user', 'UsersController');

    Route::get('user/lessons/{user}', 'UsersController@lessons');

    Route::get('user/lesson/{user}/{lesson}', 'UsersController@lesson');

});

I also created two filters for this:

Route::filter('auth.basic', function()
{
    return Auth::basic();
});

Route::filter('basic.once', function()
{
    return Auth::onceBasic('email');
});

However, since the concept of authentication through an API is still a bit vague to me, I don't know which filter is the best one to use for these?

if this is an API you are not going to log the api sender.

So I would create a new filter and add authentication by your needs:

Route::filter('apiAuth', function()
{
     $user = Input::get('user');
     $key = Input::get('key');
     if (!isset($user, $key)){
         die('error');
     }
     if(!checkDataHere){
         die('error');
     }
});

this is very basic, but this is the general idea

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM