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

524
Visualizações
Laravel Sanctum CSRF not authenticating

I have a problem understanding use of CSRF tokens with Laravel/Sanctum. As I see, there is no need to create Sanctum API token to authenticate user, and considering that my frontend (Vue) is integrated in the Laravel resources, I want to check Auth::check() in blade like so:

@if (auth()->check())
  @php
    $user_auth_data = [
        'isLoggedin' => true,
        'user' =>  Auth::user()
    ];
  @endphp
@else
  @php
    $user_auth_data = [
        'isLoggedin' => false
    ];
  @endphp
@endif

I have the

<meta name="csrf-token" content="{{ csrf_token() }}">

And in every frontend request, there is a X-CSRF-TOKEN value, but it's not authenticating.

Ofc, I'm using before login request.

.get("/sanctum/csrf-cookie")

And in API middleware I have VerifyCsrfToken.

This is how my login controller looks like:

public function login(AuthRequest $request): AuthResource
{
    $auth = Auth::user();
    $token = $auth->createToken('auth')->plainTextToken;
    $auth->setAttribute('token', $token);
    ProcessUserDevice::dispatch($auth->id, $_SERVER, $token);
    return new AuthResource($auth);
}

How could I authenticate user on frontend of vue and in main blade, is it even possible?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

CSRF is for the http requests. I mean, you will need it when you submit a from in Blade.

For API calls, you need token base authentication, which is what Sanctum provides. If you use axios to make API calls, you can use the following lines to setup it properly.

window.axios = require('axios');
window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
axios.defaults.withCredentials = true;

I assume your Sanctum setup has no issue.

about 4 years ago · Juan Pablo Isaza 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