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

2.2K
Visualizações
Get 401(Unauthenticated) error after login on laravel sanctum with nuxtjs

I have installed:

  • laravel v 7.30.4
  • nuxtjs v 2.15.7

After i login in my laravel sanctum with nuxtjs auth module, when nuxt try to get user, laravel response 401 error(Unauthenticated message).

My network status: enter image description here

My cookies status: enter image description here

api.php:

    Route::group(['middleware'=>'auth:sanctum'], function (){
       Route::get('/user', function (Request $request){
          return $request->user();
       });
    });

nuxt.config.js :

   auth:{
    strategies: {
      'laravelSanctum': {
        provider: 'laravel/sanctum',
        url: 'http://localhost:8000',
        endpoints:{
          login:{
            url:'/login'
          },
          logout:{
            url:'/api/logout'
          },
        },
        user:{
          property:false
        }
      },
    },
    redirect:{
      login: '/auth/login',
      logout:'/',
      home:'/'
    },
    cookie: {
      options: {
        sameSite: 'lax'
      },
    },
  },

LoginController.php :

    public function login(Login $request)
    {
        $user=User::where('phone_number', $request->phone_number)->first();
        if (Hash::check($request->password, $user->password)){
            $request->session()->regenerate();
            return response()->json(null, 201);
        }
        return $this->setStatusCode(422)->respondWithError('password','invalid pasword');
    }

.env :

SESSION_DRIVER=file
SESSION_LIFETIME=120
SESSION_DOMAIN=localhost

SANCTUM_STATEFUL_DOMAINS=localhost:3000

I don't know where is problem that i get 401 error!

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

I had the same issue, the solution was to add localhost:8000 (laravel domain) to SANCTUM_STATEFUL_DOMAINS

SESSION_DOMAIN=localhost
SANCTUM_STATEFUL_DOMAINS=localhost:8000,localhost:3000
over 4 years ago · Santiago Trujillo Relatório

0

you need to return your api token after logging in. and send it in every request that you use middleware('auth:sanctum') in laravel route.

more info about issuing tokens in sanctum

getting user requires token and if not provided it with throw 401 error.

over 4 years ago · Santiago Trujillo Relatório

0

on your .env you can add this SESSION_SECURE_COOKIE=false

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