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
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.
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.
Four quick questions, then a straight answer.
From your answers, this is quietly costing you traffic and nobody has the hours to chase it. Scaplet finds every affected page, drafts the fixes and tracks how AI answers mention you.
A data platform shipped 46 fixes without a ticket.
This looks handled. Re-check after a redesign, a migration or a big content push; that is when it breaks.
Your site is small and you have the time. Work through the steps on this page, starting with the pages that get the most traffic.
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.
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": truecurl -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"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:
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:
->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.
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.3 questions people ask next, answered from the sources on this page.
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.
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.
By default the auth.basic middleware treats the email column on the users table as the username.
Run the free audit, then approve the fixes. Every change can be undone.