# 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.

Canonical: https://scaplet.com/errors/http/401/laravel

Guide Error · HTTP status codes

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

401 4xx · Client error

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](#which-guard-is-rejecting-the-request)
2. [Reproduce and check it in 2 minutes](#reproduce-and-check-it-in-2-minutes)
3. [Fixes by authentication type](#fixes-by-authentication-type)
4. [What it means for SEO and crawlers](#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 with [ChatGPT](https://chatgpt.com/?hints=search&q=Summarize%20https%3A%2F%2Fscaplet.com%2Ferrors%2Fhttp%2F401%2Flaravel%20(%22401%20Unauthorized%20in%20Laravel%22)%20in%205%20bullet%20points%2C%20then%20tell%20me%20the%20one%20thing%20I%20should%20do%20next.) [Claude](https://claude.ai/new?q=Summarize%20https%3A%2F%2Fscaplet.com%2Ferrors%2Fhttp%2F401%2Flaravel%20(%22401%20Unauthorized%20in%20Laravel%22)%20in%205%20bullet%20points%2C%20then%20tell%20me%20the%20one%20thing%20I%20should%20do%20next.) [Perplexity](https://www.perplexity.ai/search?q=Summarize%20https%3A%2F%2Fscaplet.com%2Ferrors%2Fhttp%2F401%2Flaravel%20(%22401%20Unauthorized%20in%20Laravel%22)%20in%205%20bullet%20points%2C%20then%20tell%20me%20the%20one%20thing%20I%20should%20do%20next.) [![Google](/logos/ai/google-color.svg)AI Mode](https://www.google.com/search?udm=50&q=Summarize%20https%3A%2F%2Fscaplet.com%2Ferrors%2Fhttp%2F401%2Flaravel%20(%22401%20Unauthorized%20in%20Laravel%22)%20in%205%20bullet%20points%2C%20then%20tell%20me%20the%20one%20thing%20I%20should%20do%20next.)

_01_Credentials 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](/errors/http/403/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](/glossary/tokenization)

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.

_02_Fix 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
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
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
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:

_03_Your 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.

**Do not mix the two for your own SPA**The Sanctum docs say you should not use API tokens to authenticate your own first-party SPA. Use its SPA authentication features instead.

_04_Explained

## 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](/errors/http/401) for the status itself and [4xx errors](/seo-issues/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. 01 [Laravel 12.x authentication](https://laravel.com/docs/12.x/authentication) laravel.com · checked 2026-11-01
2. 02 [Laravel 12.x Sanctum](https://laravel.com/docs/12.x/sanctum) laravel.com · checked 2026-11-01
3. 03 [Laravel 12.x error handling](https://laravel.com/docs/12.x/errors) laravel.com · checked 2026-11-01
4. 04 [How HTTP status codes affect Google's crawlers](https://developers.google.com/crawling/docs/troubleshooting/http-status-codes) developers.google.com · checked 2026-11-01

Questions

## Asked a lot.

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

_01_ Why 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.

_02_ What 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.

_03_ How do I set the Basic auth username?

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

## Keep learning

[All errors](/errors)

[Learn 403 Forbidden in Laravel HTTP status codes](/errors/http/403/laravel) [Related 4xx errors Status codes and links](/seo-issues/4xx-errors) [Read next HTTP 401 Unauthorized HTTP status codes](/errors/http/401) [Do Run a free site audit Free, about two minutes](/site-audit)

Other 401 errors
[401 Unauthorized in IIS](/errors/http/401/iis) [401 Unauthorized in Spring Boot](/errors/http/401/spring-boot) [401 Unauthorized in the WordPress REST API](/errors/http/401/wordpress) [HTTP 401 Unauthorized](/errors/http/401)

More in HTTP 401 Unauthorized
[401 Unauthorized in IIS](/errors/http/401/iis) [401 Unauthorized in Laravel](/errors/http/401/laravel) [401 Unauthorized in Spring Boot](/errors/http/401/spring-boot) [401 Unauthorized in the WordPress REST API](/errors/http/401/wordpress)

Terms on this page
[Tokenization](/glossary/tokenization)

Free · 2 minutes

## Tools point. Agencies advise. _This one ships._

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

[Audit my site free](/site-audit)

Free, no card 60 seconds Nobody will call you

- **46** fixes shipped without a ticket Brightpipe
- **0→7** ChatGPT citations in six weeks Paylane
