Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

182
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda