简体   繁体   中英

Laravel 5.4 Combined Api route group with auth middleware?

How do I combine the api route group with my auth to have something like this:

Route::group(['middleware' => 'auth'], function () { 
   ... 
});

Thus if I call the default api route:

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

app/api/user

I get no return value.

I am new to api routes so what am I doing wrong.

I could not found a tutorial for api Laravel 5.4

Something like this

Route::group(['middleware' => 'auth:api'], function(){
   Route::get('user', function(Request $request) {
       dd($request->user());
   });
});

Passing your token via ajax

var token = <?php json_encode(Auth::user()->api_token); ?>;
$.ajax({
    url:'http://...',
    headers: {
        'Authorization': 'Bearer ' + token
    },
    method: 'POST'
    ...
})
Route::middleware('auth:api')->get('/user', function () { 
     return Auth::user(); 
});

or

Route::get('/user', function () { 
         return Auth::user(); 
})->middleware('auth');

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