Get authenticated User in API controller in Laravel

孤街醉人 提交于 2021-01-07 06:36:51

问题


I want to fetch the Authenticated User's data in API controller. How to do that?
Here is my API\CompanyController

public function selected_company(){
        return Auth::user()->id;
    }

The error I got through HTTP request... Error Image Here


回答1:


To convey the comment in a comprehensible manner,

Either your controller should have a middleware, like

public function __construct()
{
    $this->middleware('auth:api');
}

Or your route to the api should be passed through the middleware

Route::get('your-api-endpoint')->middleware('auth:api');


来源:https://stackoverflow.com/questions/65016677/get-authenticated-user-in-api-controller-in-laravel

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!