1

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

2
  • let the controller method pass through a middleware like auth:sanctum or auth:api, depends on what package you are using for api authentication. Commented Nov 26, 2020 at 5:51
  • I have updated return Auth::user()->id; to return auth('api')->user() but also it is not working Commented Nov 26, 2020 at 13:12

1 Answer 1

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');
Sign up to request clarification or add additional context in comments.

4 Comments

Are you passing the API Token in the Headers ? Authorization: Bearer <token>
Maybe you should read more about how to authenticate user and pass the tokens in header.
Will you please recommend the resource site?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.