My laravel application route configured on routes/api.php is.
<?php
use Illuminate\Http\Request;
Route::post('see_all_product', 'API\ProductController@see_all_product');
?>
Issue is i want to sent list of product but if user authenticated then send product favorite flag 1, and if not authenticated then send return favorite 0
But both case send product list with favorite flag.
If i logged in with my user id and password and send request for see_all_product that time i m getting blank user.
$user = $request->user();
But if i set route like below i m getting user details.
<?php
use Illuminate\Http\Request;
Route::group(['middleware' => 'auth:api'], function(){
Route::post('see_all_product', 'API\ProductController@see_all_product');
});
?>
Now issue is how can i get details if authorization set in the header with same api.
<?php
use Illuminate\Http\Request;
Route::post('see_all_product', 'API\ProductController@see_all_product');
?>
My see_all_product Function
public function see_all_product(Request $request){
try {
$user = $request->user();
} catch (Exception $ex) {
Log::error($ex);
}
}
API is same for both authenticated and guest user.
I pass authorization token in both case but middleware route i get user details but non middleware route i dont get user information.
Please guide me where i can miss something?
I think you can do it by the way instead of $request->user()
:
if (auth('api')->check()) {
$user = auth('api')->user();
}
Not working blank user. if i change my route to this then working but facing issue with non authorized user.
<?php use Illuminate\Http\Request; Route::group(['middleware' => 'auth:api'], function(){ Route::post('see_all_product', 'API\ProductController@see_all_product'); }); ?>
Then you have more deeply problem because this should be work on unathorized endpoints. What is your guard and provider? Do you have any specific settings at authentication settings?
I changed my reply. Do you check and try again like this?