Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

181
Vistas
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?

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

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'.

over 4 years ago · Santiago Trujillo Denunciar

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)

over 4 years ago · Santiago Trujillo Denunciar

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.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda