401 vs 403: Pick the Right Status for a Refused Request
Return 401 Unauthorized when the request has no valid credentials: no token, an expired session, a bad signature. Logging in could fix it, and the response must carry a WWW-Authenticate header. Return 403 Forbidden when you know who the caller is and they still may not do this, so signing in again changes nothing.
| 401 Unauthorized | 403 Forbidden | |
|---|---|---|
| Really means | Not authenticated | Not permitted |
| Will logging in help? | Yes | No |
| Required header | WWW-Authenticate | None required |
| Typical cause | Missing or expired token or session | Role or ownership check failed |
| Client should | Sign in or refresh the token, then retry | Stop and show an access message |
Updated
Answer four questions about the request your server is refusing. The picker returns the status to send, the header that goes with it, and a one-line reason you can put in a code comment. It also covers the case where you would rather not admit the resource exists.
Which status should I return?
Did the request carry credentials (session cookie, token, Authorization header)? Yes
Were those credentials valid and unexpired? Yes
Is this caller allowed to perform this action on this resource? No
Should callers without access be kept from learning the resource exists? No
Send this
HTTP/1.1 403 Forbidden
Content-Type: application/json
{"error": "forbidden"}The caller is known and lacks permission. Signing in again will not help.
The check in code
Express middleware: 401 first, then 403
verifyToken stands in for your own session or JWT check. The order matters: authenticate, then authorise.
function requireRole(role) {
return async (req, res, next) => {
const header = req.get("Authorization") || "";
const token = header.startsWith("Bearer ") ? header.slice(7) : null;
if (!token) {
res.set("WWW-Authenticate", 'Bearer realm="api"');
return res.status(401).json({ error: "authentication_required" });
}
const user = await verifyToken(token).catch(() => null);
if (!user) {
res.set("WWW-Authenticate", 'Bearer realm="api", error="invalid_token"');
return res.status(401).json({ error: "invalid_token" });
}
if (!user.roles.includes(role)) {
return res.status(403).json({ error: "forbidden" });
}
req.user = user;
next();
};
}Front end: react differently to each
One code path sends the user to sign in; the other explains the refusal.
const res = await fetch("/api/reports", { credentials: "include" });
if (res.status === 401) {
window.location.assign("/login?next=/reports");
} else if (res.status === 403) {
showMessage("Your account does not have access to reports.");
} else {
render(await res.json());
}
Two different questions
Every protected request answers two questions in order. Who is calling? That is authentication, and failing it gives 401. Is this caller allowed to do this? That is authorisation, and failing it gives 403.
Keeping them apart helps clients. A 401 tells a front end to send the user to sign in. A 403 tells it that signing in again will not help and an access message belongs on screen instead.
What the specification adds
When a request already carried credentials and still gets a 401, the server is saying those credentials were refused. For a 403 the server may explain the reason in the body, or say nothing about it at all.
How it works
- Say whether the request arrived with any credentials at all.
- Say whether those credentials checked out.
- Say whether this caller has permission for this action on this resource.
- Decide whether the resource's existence should stay hidden from callers without access.
Tips
- The name Unauthorized is a historical accident. 401 is about authentication, who you are; 403 is about authorisation, what you may do.
- In a single page app, treat 401 as a trigger to refresh the token or show the sign-in screen, and 403 as a message the user cannot fix by signing in.
- Use 404 instead of 403 for private resources whose existence is itself sensitive, such as another customer's invoice id.
- Log 403s with the user id and the permission that failed. They are the quickest way to spot a role that was set up wrong.
Frequently asked questions
What is the main difference between 401 and 403?
401 says the server does not know who you are, or did not accept the proof you sent. 403 says it knows who you are and the answer is still no.
Must a 401 include the WWW-Authenticate header?
Yes. RFC 9110 says a server generating a 401 response must send at least one WWW-Authenticate header field with a challenge, such as Bearer for token APIs.
Should an expired token get 401 or 403?
401. The credentials are no longer valid, and fresh ones would fix it. A Bearer challenge can say so with error="invalid_token".
Can I return 404 instead of 403?
Yes. The specification lets a server send 404 instead of 403 to hide whether the resource exists. It is a common choice for ids that belong to other users.
Does a 403 mean my IP address is blocked?
It can. Servers send 403 for any refusal they consider final for this request: IP rules, missing file permissions, a firewall rule or a failed role check. The response body or server log usually says which.
Related: HTTP status codes reference, Next.js authentication guide, What is a JWT, OAuth explained, HTTP 405 Method Not Allowed.
Build your site free or see all free tools. On We.Inc you can build your site free, with no card and no trial needed to start. AI building is on paid plans from $19 a month.
More in Guides
- 403 Forbidden: Why the Server Refuses and How to Fix It
- 404 Not Found: Why Pages Go Missing and How to Fix It
- 404 Page Examples: 20 Designs With Copy-Paste Code
- 409 Conflict: When a Request Clashes With Current State
- 500 Internal Server Error: Finding the Real Cause
- 502 Bad Gateway: Meaning and Fixes
- 503 Service Unavailable: Meaning, Causes and Fixes
- 504 Gateway Timeout: What Timed Out and How to Fix It
Product
Who It's For
- For Agencies
- For Freelancers
- For Hosting Providers
- For SaaS Platforms
- For Startups
- For Financial Services
- For Healthcare
Features
Resources
- Blog
- Help Center
- Free Business Tools
- Free UI Components
- Website Templates
- Template Collections
- Website Examples
- Guides
- Comparisons
- Alternatives
- Best Lists
- Reviews
- Pricing Guides
- Use Cases
- Glossary & Guides
- Integrations
- Developers
- Benchmark
- Windows App