Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

179
Views
Laravel 5.4 Passport Log user action (Optional auth:api)

I am using Laravel Passport (auth:api), all works well however I've came with the idea to log record user requests on a specific route.

When an GET request is made to /movie/65 I would like to store in movie_view the following data: user_id, movie_id (if the user is logged in)

However in my controller I am unable to call $request->user() without setting auth:api middleware.

What is the best practice you recommend to achieve this?

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Best way to do this is:

if (\Auth::guard('api')->check()) {
    $user = \Auth::guard('api')->user();
}

you have to setup the guard to 'api', the default one is 'web'.

about 4 years ago · Santiago Trujillo Report

0

Default Auth Type should be set to 'api' in config/auth.php

Source: https://laracasts.com/discuss/channels/general-discussion/optional-authentication-for-api?page=0 (it was very hard to find)

about 4 years ago · Santiago Trujillo Report

0

You can create a custom middleware as suggested here -- https://laracasts.com/discuss/channels/laravel/how-to-handle-route-with-optional-authentication. Then apply api guard to it. Let say you assign that custom middleware as auth.optional in Kernel.php, then you can use that middleware as auth.optional:api (with that api guard). Then you can access user thru $request->user() in your case above.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!