Authentication & Authorization

JWT vs OAuth: Understanding the Difference

JWT and OAuth are often confused because they appear together — but they solve completely different problems. JWT is a token format. OAuth is an authorization framework. Once you see that distinction, everything else clicks.

Why People Confuse Them

The confusion exists because OAuth implementations often use JWT as their token format. When you "Login with Google" (OAuth), Google returns an access token — and that token is frequently a JWT. So developers see JWT and OAuth together constantly and start treating them as the same thing.

Think of it this way: OAuth is a shipping company. JWT is a box. The shipping company decides how to ship things. The box is just a container format. You can use OAuth without JWT (use opaque string tokens instead). You can use JWT without OAuth (just issue tokens yourself directly).

What JWT Is

JWT (JSON Web Token) is a compact, self-contained way to encode data as a signed string. A JWT consists of three Base64URL-encoded parts separated by dots:

// Structure
header
.
payload
.
signature
// Example token (truncated)
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ1c2VyXzEyMyIsImVtYWlsIjoiYWxpY2VAZXhhbXBsZS5jb20iLCJleHAiOjE3MTcwMDAwMDB9.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c

The payload contains claims — key-value pairs like user ID, email, role, and expiration time. The signature proves the token wasn't tampered with.

// Decoded payload
{
"sub": "user_123",
"email": "[email protected]",
"role": "admin",
"exp": 1717000000
}

The key property: a server can verify a JWT without any database lookup. It just checks the signature using its secret key. This makes JWTs stateless and fast.

What OAuth Is

OAuth 2.0 is an authorization framework — a set of rules that lets a user grant a third-party application limited access to their account on another service, without sharing their password.

The classic example: a project management app wants to read your Google Calendar. You don't give it your Google password. Instead, Google asks if you consent, then gives the app a token that only lets it read your calendar (not change your password, not access other data).

OAuth Authorization Code Flow
  1. 1.User clicks "Login with Google" in your app
  2. 2.Your app redirects user to Google's authorization endpoint with your client_id and requested scope
  3. 3.User logs in to Google and approves the permission request
  4. 4.Google redirects back to your app with a short-lived authorization code
  5. 5.Your app's backend exchanges the code for an access token (and optionally a refresh token)
  6. 6.Your app uses the access token to make API calls on behalf of the user

OAuth defines who the parties are (Resource Owner, Client, Authorization Server, Resource Server), what flows are used (Authorization Code, Client Credentials, Device Code, etc.), and how tokens are exchanged. OAuth says nothing about the token's format — that's where JWT comes in.

Side-by-Side Comparison

PropertyJWTOAuth 2.0
TypeToken format / data encoding standardAuthorization framework / protocol
Defined byRFC 7519RFC 6749
PurposeEncode claims in a compact, verifiable stringDelegate authorization without sharing credentials
ContainsHeader, payload, signatureA multi-step flow with multiple parties
Stateless?Yes — server verifies signature, no DB neededDepends on token format used
Revocable?Not by default — token is valid until expiryYes — auth server can invalidate tokens
Use without the other?Yes — JWT alone for simple authYes — OAuth can use opaque tokens instead
Common together?Yes — OAuth often issues JWT access tokensYes — OIDC extends OAuth and always uses JWT
Security scopeToken integrity and claimsAuthorization flow security
When to use aloneInternal APIs, simple login systemsThird-party access, social login, multi-tenant

How They Work Together: OpenID Connect

OAuth 2.0 handles authorization (can this app access this resource?) but not authentication (who is this user?). OpenID Connect (OIDC) is a layer on top of OAuth 2.0 that adds authentication.

OIDC mandates the use of a JWT ID token — a JWT that carries identity information (name, email, profile picture). This is why JWT and OAuth are so often seen together: every OIDC implementation (Google login, Microsoft login, Auth0) returns a JWT.

The pattern in modern systems:
  • • OAuth 2.0 handles the authorization flow and token issuance
  • • OIDC adds identity (ID token as a JWT)
  • • Access tokens may also be JWTs for stateless resource server verification
  • • Your backend uses the JWT's claims directly without contacting the auth server

When to Use Each

Use JWT (alone, no OAuth)
  • • Building a simple login system for your own app
  • • Internal microservices communicating with each other
  • • APIs where you control both client and server
  • • Short-lived tokens where revocation isn't critical
  • • You need stateless auth to avoid DB lookups on every request
Use OAuth (with or without JWT)
  • • Social login ("Login with Google/GitHub")
  • • Letting third-party apps access user data
  • • Building a public API that developers integrate
  • • You need fine-grained scopes and token revocation
  • • Multi-tenant SaaS or enterprise auth (SSO)

Common Mistakes

Storing sensitive data in JWT payload
The JWT payload is Base64-encoded, not encrypted. Anyone with the token can decode and read the payload. Never store passwords, SSNs, or sensitive PII in JWT claims. Use encryption (JWE) if you must store sensitive data.
Using long JWT expiry times to avoid refresh logic
JWTs can't be revoked without extra infrastructure. A 7-day JWT means a compromised token is valid for 7 days. Use short expiry (15–60 minutes) + refresh tokens. If you need easy revocation, use a token blocklist or switch to opaque tokens.
Implementing OAuth from scratch
OAuth has many subtle security requirements (state parameter for CSRF, PKCE for public clients, secure redirect URI validation). Use a battle-tested library like Passport.js, NextAuth.js, Auth.js, or a managed service like Auth0/Clerk.
Treating OAuth access tokens as identity tokens
An access token proves authorization to access a resource, not the identity of the user. For identity, use the ID token (OIDC) or call the userinfo endpoint. This confusion causes subtle security bugs.

Frequently Asked Questions

Can you use JWT without OAuth?

Yes. JWT is just a token format. A common pattern is to issue a JWT at login, store it client-side, and send it in the Authorization header on every request. Your server verifies the signature without any OAuth involvement. No authorization server, no token exchange — just a signed token you issue yourself.

Does OAuth require JWT?

No. OAuth is format-agnostic. Access tokens can be opaque random strings that the resource server validates by calling the authorization server's introspection endpoint. JWT just happens to be a popular choice because it allows stateless validation without that round-trip.

Is OAuth more secure than JWT?

They solve different problems and can't be directly compared for security. OAuth provides a secure delegation flow. JWT provides a secure encoding structure. OAuth with JWT access tokens gives you both: a secure flow and stateless token validation. Both can be implemented insecurely if misused.

What is the difference between JWT and OAuth2?

JWT is a token format (RFC 7519). OAuth 2.0 is an authorization framework (RFC 6749). They operate at different levels of abstraction. JWT defines how data is encoded and signed. OAuth 2.0 defines how authorization flows work and how applications request access. OAuth 2.0 often uses JWT as the token format, but doesn't require it.

What is OpenID Connect vs OAuth?

OAuth 2.0 handles authorization (can this app do this?). OpenID Connect (OIDC) extends OAuth 2.0 to handle authentication (who is this user?). OIDC adds a standardized ID token (always a JWT) and a userinfo endpoint. "Login with Google" uses OIDC — you get both an access token (OAuth) and an ID token (OIDC/JWT).

Key Takeaways

  • JWT is a token format. OAuth is an authorization framework. They solve different problems.
  • OAuth often uses JWT as its token format, which is why they appear together.
  • Use JWT alone for simple internal auth. Use OAuth when third parties need delegated access.
  • OpenID Connect (OIDC) extends OAuth 2.0 with identity and always uses JWT ID tokens.
  • Never put sensitive data in a JWT payload — it's encoded, not encrypted.
  • Use short JWT expiry times and refresh tokens, not long-lived tokens.

Related Resources