In this lesson you will
- Tell authentication from authorization and 401 from 403
- Send Basic and bearer credentials and choose between sessions and tokens
- Read a JWT, verify its signature and avoid the common JWT mistakes
401, WWW-Authenticate and Basic auth
Authentication answers "who are you?" and authorization answers "are you allowed to do this?". HTTP has a status code for each failure. 401 Unauthorized, despite its name, means not authenticated: send credentials and try again. The server says which kind it accepts in WWW-Authenticate, here Basic realm="Fake Realm". 403 Forbidden means the server knows who you are and the answer is still no, so sending the same credentials again will not help.
Basic auth is the oldest scheme: the client joins username and password with a colon, encodes the result in base64 and sends it in every request. The recording shows ali:s3cret becoming YWxpOnMzY3JldA== and base64 -d turning it straight back. Base64 is an encoding, not encryption, so anyone who sees the request sees the password. That is acceptable only over HTTPS, and mostly for scripts and internal tools. Browsers also show their own grey login box for it, which you cannot style, and there is no clean way to log out.
HTTP/2 401
access-control-allow-credentials: true
access-control-allow-origin: *
access-control-expose-headers: Retry-After, RateLimit, RateLimit-Policy, WWW-Authenticate
content-length: 0
www-authenticate: Basic realm="Fake Realm"
x-content-type-options: nosniffHTTP/2 200
{
"authenticated": true,
"user": "ali",
"authorized": true
}> Authorization: Basic YWxpOnMzY3JldA==
$ echo YWxpOnMzY3JldA== | base64 -d
ali:s3cretThe password is a demo value for httpbin, which accepts whatever user and password you put in the URL. Never paste real credentials into a test service.
Sign in free to read the full lesson
You are reading the free preview. Create a free account (it takes one minute, no password) to unlock the rest of this lesson and track your progress.
Unlocks after sign-in:
- 2 more sections with live code examples
- Common mistakes to avoid
- Hands-on coding exercise with automatic checks
- Practice quiz and the graded lesson test
Free forever. No card, no paywall.
Questions people ask
Should I store a JWT in localStorage or in a cookie?
For a website calling its own API, an HttpOnly, Secure, SameSite cookie is safer because scripts cannot read it. localStorage is easier for APIs on other domains but exposes the token to any XSS bug, so keep such tokens short-lived.
How do I log a user out when I use JWTs?
Delete the token on the client, and on the server keep access tokens short and revoke the longer-lived refresh token. For instant logout you need a server-side denylist, which brings back some of the state that sessions already had.
Is OAuth the same thing as JWT?
No. OAuth 2.0 is a process for getting an access token, for example "Sign in with Google" or giving an app access to your calendar. JWT is one possible format for tokens. OAuth tokens may be JWTs or random strings.
How long should an access token last?
Common choices are 5 to 60 minutes, paired with a refresh token that lasts days or weeks and can be revoked. Shorter is safer because a stolen token stops working sooner.
Is an API key the same as a bearer token?
Both are secrets sent with each request, but an API key usually identifies a project or account and rarely expires, while a bearer token usually stands for a logged-in user for a short time. Keep API keys on the server, never in front end code.
Sources
Facts in this lesson were checked against these official pages on 30 November 2026.
- MDN: HTTP authentication
- RFC 7519: JSON Web Token (JWT)
- RFC 6750: OAuth 2.0 Bearer Token Usage
- OWASP Session Management Cheat Sheet
- MDN: SubtleCrypto.sign()
Code examples stay in English and run in a sandbox on your device. If you spot a mistake, tell us through the contact page.