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

2.2K
Views
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 answers
Answer question

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 Report

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 Report

0

on your .env you can add this SESSION_SECURE_COOKIE=false

over 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!