Free auditStart free
GuideError · HTTP status codes

401 Unauthorized in Laravel

Why a Laravel route answers 401: missing Bearer token, an expired Sanctum session, a stateful-domain mismatch or Basic auth. Reproduce it and fix by auth type.

3 min
read
4
sources
Oct 31, 2026
updated
Scaplet Editorial Team
author
The short answer

A Laravel 401 means the request carried no valid credentials for the guard protecting that route. Which credentials count depends on the guard: a session cookie, a Sanctum Bearer token in the Authorization header, or HTTP Basic. Identify the guard on the route, then check that the client sends exactly what that guard reads.

  1. Which guard is rejecting the request
  2. Reproduce and check it in 2 minutes
  3. Fixes by authentication type
  4. What it means for SEO and crawlers
30-second check

Where does your site stand?

Four quick questions, then a straight answer.

1/4Have you checked your own site for this?
Summarize this page withChatGPTClaudePerplexityGoogleAI Mode
01Credentials each Laravel approach expects

Which guard is rejecting the request

A 401 is about identity: the server could not tell who is calling. If the user is known but not allowed, you want 403 in Laravel. The routes in the same app can use different guards, so start with the middleware on the failing route.
Approach

Session login (web)

Route middleware
auth
What the client must send
The session cookie from a login; guests are redirected to the login named route
Source in the docs
Authentication, protecting routes
Approach

Sanctum API token

Route middleware
auth:sanctum
What the client must send
Authorization header with the token as a Bearer token
Source in the docs
Sanctum, issuing API tokens
Approach

Sanctum SPA

Route middleware
auth:sanctum
What the client must send
Session cookie plus Accept: application/json and a Referer or Origin header; XSRF-TOKEN echoed as X-XSRF-TOKEN
Source in the docs
Sanctum, SPA authentication
Approach

HTTP Basic

Route middleware
auth.basic
What the client must send
Username and password; the email column is the default username
Source in the docs
Authentication, HTTP Basic
Approach

Explicit

Route middleware
abort(401)
What the client must send
Nothing helps: your code decided
Source in the docs
Error handling, HTTP exceptions

The authentication docs say the auth middleware redirects an unauthenticated user to the login named route, and redirectGuestsTo changes that target. A browser therefore sees a redirect, not a 401. A 401 shows up for clients that do not follow a redirect to a login page, such as API calls, or from Basic auth and Sanctum.

02Fix it on your stack

Reproduce and check it in 2 minutes

These calls run against httpbin, which implements the same Bearer and Basic conventions, so you can see the exact success and failure behavior before testing your own API.
Public endpoint: Bearer token present versus missing
curl -s -o /dev/null -w "%{http_code}\n" https://httpbin.org/bearer
# Expect: 401 (no Authorization header)
curl -s -H "Authorization: Bearer abc" https://httpbin.org/bearer
# Expect: JSON with "authenticated": true
Public endpoint: Basic auth, right and wrong password
curl -s -o /dev/null -w "%{http_code}\n" -u user:pass https://httpbin.org/basic-auth/user/pass
# Expect: 200
curl -s -o /dev/null -w "%{http_code}\n" -u user:wrong https://httpbin.org/basic-auth/user/pass
# Expect: 401
curl -sI https://httpbin.org/basic-auth/user/pass | grep -i www-authenticate
# Expect: www-authenticate: Basic realm="Fake Realm"
Your own API: token versus no token
curl -i -H 'Accept: application/json' https://your-app.example/api/user
curl -i -H 'Accept: application/json' -H 'Authorization: Bearer YOUR_TOKEN' https://your-app.example/api/user
# Expect: the first call fails authentication, the second returns the user JSON.
# If the second still fails, the token or the guard is the problem.

Now repeat the first pair against your own API. Replace the URL and token. Add the Accept header so Laravel treats the call as a JSON client:

03Your call

Fixes by authentication type

For API tokens, Sanctum hashes tokens with SHA-256 before storing them, so the plain text value is only available when it is created. If a client lost it, issue a new one:
If this fits

Use Bearer tokens if

  • The client is a mobile app or third-party consumer
  • You want tokens users can revoke one by one
Or

Use SPA cookie auth if

  • The frontend is your own and shares a top-level domain with the API
  • You want CSRF protection and no token stored in JavaScript
Issue a token and return the plain text once
Route::post('/tokens/create', function (Request $request) {
    $token = $request->user()->createToken($request->token_name);

    return ['token' => $token->plainTextToken];
});

Also check expiry. By default Sanctum tokens never expire, so if one stops working, someone set the expiration option or deleted the token. The docs show revoking with $user->tokens()->delete().

For an SPA, the Sanctum docs require a shared top-level domain, the stateful domains list, and a call to /sanctum/csrf-cookie before login. They also note that when a session expires from inactivity, later requests may get a 401 or 419, and the SPA should send the user to its login page. In bootstrap/app.php the middleware method is:

bootstrap/app.php
->withMiddleware(function (Middleware $middleware): void {
    $middleware->statefulApi();
})

If the port is part of the URL while developing (for example 127.0.0.1:8000), the docs say to include the port number in the stateful domain. A request from a host that is not on the list is treated as stateless, so the cookie is ignored and you get the 401.

04Explained

What it means for SEO and crawlers

Google puts 401 in the same bucket as other 4xx codes except 429: content is ignored, and it warns not to use 401 or 403 to limit crawl rate. Private API endpoints returning 401 to crawlers is fine. A public page behind auth.basic, for example a staging site left on, will not be indexed. The reverse also bites: a page you want indexed sitting in an auth group sends crawlers to the login redirect. See 401 Unauthorized for the status itself and 4xx errors for site-wide checks.
Sources · 4

Every fact, linked and dated.

Official docs first. The date is when we last opened each page.
  • laravel.com
  • google.com
  1. 01Laravel 12.x authenticationlaravel.com · checked 2026-11-01
  2. 02Laravel 12.x Sanctumlaravel.com · checked 2026-11-01
  3. 03Laravel 12.x error handlinglaravel.com · checked 2026-11-01
  4. 04How HTTP status codes affect Google's crawlersdevelopers.google.com · checked 2026-11-01
Questions

Asked a lot.

3 questions people ask next, answered from the sources on this page.

01Why does my Laravel API return 401 with a valid Sanctum token?

Check that the client sends the token in the Authorization header as a Bearer token, that the route uses auth:sanctum, and that the token was not revoked or given an expiration. Tokens are hashed in the database, so only the plain text value shown at creation works.

02What is the difference between 401 and 419 in Laravel?

Sanctum's docs say an expired session may produce a 401 or a 419. Laravel's error docs describe 419 as the response for an invalid CSRF token.

03How do I set the Basic auth username?

By default the auth.basic middleware treats the email column on the users table as the username.

Free · 2 minutes

Tools point. Agencies advise. This one ships.

Run the free audit, then approve the fixes. Every change can be undone.

Free, no card60 secondsNobody will call you
  • 46 fixes shipped without a ticket Brightpipe
  • 0→7 ChatGPT citations in six weeks Paylane